简体   繁体   中英

UIVIew resizing in Storyboard and NIB difference

community. There's some strange UIView resizing thing, that i can't understand.

Let's say, we have simple Single-View Application with Main.storyboard. In storyboard i'm creating RedView . It has default size 600x600. And all options are set by default. I'm not changing anything.

Now, i am creating BlueView.xib with BlueView , and it has the same size and the same options as RedView. Also i am creating BlueViewControler class, and setting it as owner in BlueView.xib. BlueViewControler is empty.

RedView includes BlueView as a child like this:

@implementation RedViewController
{
    BlueViewController *_blueViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
    [self.view addSubview:_blueViewController.view];
}

@end

And now, when i am launching application on iPhone 5 Simulator, RedView becomes 320 * 568, and Blue view becomes 600 * 600.

2015-08-28 13:32:17.937 TestViewSize[3087:77218] RedView frame = {{0, 0}, {320, 568}}
2015-08-28 13:32:17.938 TestViewSize[3087:77218] BlueView frame = {{0, 0}, {600, 600}}

On the screen they also look differently. So my question is:

  1. Why do RedView, created in storyboard, autoresizes? I can understand, why Blue view is 600*600, but why Red adjusts to screen size? Who tells it to do this?

  2. What the difference between creating view in XIB and in storyboard related to sizing views, not anything else. Why do storyboard resize its view, and .XIB do not?

My project Created in Xcode 6, target is 8.0, using of Autolayout and Size Classes are enabled.

Such an old question, but still someone might not quite understand such behaviour, like me when faced it firstly.

UIViewController in storyboard has option "Resize View From NIB" , while UIView in xib hasn't. By default this option is on, that makes view controller's view resized to screen size when loading, even before view is added to views hierarchy.

I think that is becaus of Size Classes According to apple docs

People generally want to use their favorite apps on all their devices and in any orientation. In iOS 8 and later, you use size classes and Auto Layout to help you meet this expectation by defining how the layout of screens, view controllers, and views should adapt when the display environment changes.

So when you test it on iPhone55 your redView which is storyboard has wAnyhAny resizes itself while blue view which you are adding as subView remians same. Now you haven't used any constarint to specify it's width or height si it's still 600x600

EDIT This might be better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM