UIView addsubview after orientation change: Tell view to resize

后端 未结 4 2142
别那么骄傲
别那么骄傲 2021-01-05 09:08

I have a UIView as a XIB in Portrait mode.

This view is added programmatically to the viewcontroller like this:

NSArray *nibObjects = [[NSBundle main         


        
相关标签:
4条回答
  • 2021-01-05 09:51
    [self.view addSubview:viewSpinner];
    viewSpinner.frame = self.view.frame;
    [viewSpinner setNeedsLayout];
    

    This works for me (Y)

    0 讨论(0)
  • 2021-01-05 09:53

    Often a NIB/XIB file contains a UIViewController that takes care of all of this. In this case, since their is no view controller (in the NIB/XIB) you need to take over its post-load duties.

    Calling layoutSubviews directly, or indirectly via setNeedsLayout or layoutIfNeeded won't do you much good because the default implementation does nothing.

    Assuming you want input view to fill the bounds of self.view you do the following:

    InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
    [self.view addSubview:inputView];
    inputView.frame = self.view.bounds;
    [inputView show];
    

    All the resize masks of the sub-views must be correctly set for this to work and, obviously, if you don't want to fill the full bounds you may want to adjust the frame.

    0 讨论(0)
  • 2021-01-05 10:07

    See this related question:

    When do autoresizing masks take effect in iOS?

    So after loading your new view from the nib, and adding as a subview to self.view, try calling setNeedsLayout.

    0 讨论(0)
  • 2021-01-05 10:13

    I don't know if you still have this issue.

    Let's say you have the following architecture:

    • window
      • subviewcontroller

    (you implemented shouldautorotate correct to answering the wanted orientations)

    Into this subviewcontroller you want to add the views of new UIViewControllers by just calling the addSubview function.

    Instead of implementing the bounds manipulation in shouldautorotate, you should implement it in

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
      self.newUIViewController.view.frame = self.view.bounds;
    }
    

    didRotateFromInterface... is called after shouldRotate. In this function the bounds are already setup correctly.

    This way you don't need so much manipulation by code.

    0 讨论(0)
提交回复
热议问题