简体   繁体   中英

UIViews with a frame of 600x600 pt on iphone 6 plus with size classes

I am working on an application that is used on all devices (iPad, iPhone). I use different xib files for iPad and iPhone platforms (just because original app was iPad only and those work great (without any autolayout)).

My iPhone xib files use autolayout and size classes. I have starting screen, i have launch images set okay. When app starts my view frame is 600x600 in viewDidLoad but its ok, i am not doing anything with it until viewWillAppear.

At this point the frame for xib file is correct (for iphone5 and iphone 6 and iPhone 6+). But i have few other views in this xib that are parallel (not subview to any other view) with the main view. And all these other views have a frame of 600x600.

I can set the frame explicitly but then i must resize each subview manually...

If i shut down size classes and use only autolayout, the frame is always iphone 5s size.

PS I just added all possible launch images (correct sizes) removed size classes and sizes are as follows: Main view - frame = (0 0; 667 375)

View next to it in xib (not subview, but it must also be full screen view) - frame = (0 0; 568 320)

All view sizes are intristic and landscape.

Edit 1 So my question is: How can other views in xib initialise in correct frame? Not 600x600.

If I usee "Debug in view hierarchy" i can see that all the views that are root views (not as any subviews) except for the main "view" are not right size. Even though screen bounds are reported correctly, views remain 600x600 size.

Edit 2 This question asks similar question: iOS 8 top layout guide auto layout problems But no answer has been given that would universally solve the problem.

If i add all those other views as subviews to my main view, frame gets sized correctly (thanks to autolayout), but this is not an option for me since i cannot have them all as subviews at launch.

So this is how i made it work for me:

As soon as i add my subview which has bad frame, i add autolayout constraints to it like this:

[self.view addSubview:_subviewFromSameXib];  //naming just for example

_subviewFromSameXib.translatesAutoresizingMaskIntoConstraints = NO;


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0.0]];

This does the following: After adding my subview it adds new constraints that tell this subview that it must size according to its superview.

This fixes my problem but does not solve the main issue: multiple views in xib do not get sized according to screen bounds. Only the first one does.

I am using xibs not storyboards btw. And i should add that playground displays all views correctly on all devices. Only on runtime they get messed up.

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