简体   繁体   中英

Portrait view in a landscape oriented device

I'm currently working on a customViewController without any interface designed in interface builder. Just so you know, my goal is to achieve a layerViewController with a container behaviour.

Everything is going smoothly, my contained viewControllers behave correctly, but there's just one thing : I want all of this to be printed in landscape mode only .

So in my init method I programmatically set the frame of my customViewController like this :

self.view.frame = [UIScreen mainScreen].applicationFrame;

and tell it that:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

At this point everything seems fine, but there's the result:

在此处输入图片说明 While the device understands that it is only allowed to be in landscape mode, it definitely gives me a portrait oriented view. (if i remove the supportedInterfaceOrientations method the view just display in portrait mode fine).

Why is that happening?

EDIT

I did try to add programmatically set constraints like the following :

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];

NSLayoutConstraint *botConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];

NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];

NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view.superview attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];

[self.view addConstraints:@[topConstraint, botConstraint, leadingConstraint, trailingConstraint]];

But no more satisfying result :(

EDIT 2

I tried to play with myViewController.view.frame and myViewController.view.bounds but nothing helps, my frame is never set to the correct landscape one. I'm kinda lost on this one ...

You need to use Autolayout feature provided in the xcode to properly arrange the UI components so that the components are autoarranged when the device orientation is in landscape mode.

http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

Check out the above tutorial to understand and learn about the AutoLayout feature.

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