UIView addsubview after orientation change: Tell view to resize

后端 未结 4 2173
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题