Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

ios8 - How do we dictate app orientation in iOS 8?

In iOS 7 we said:

// ViewController1:

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return YES;
}

// ViewController2, presented by modal segue from button in ViewController1

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return YES;
}

The result was that the app appeared in landscape in view controller 1 and in portrait in view controller 2.

That code works fine in iOS 7, including the iOS 7 simulators in Xcode 6. But it no longer works in iOS 8. There are two problems:

  • View Controller 1's view is appearing in landscape, but the simulator is not automatically rotating (might just be a simulator bug) and (this is the really important part) the view is not automatically being resized, so it is too narrow for the screen (there's a big black area to its right).

  • View Controller 2's view is appearing in the same orientation as View Controller 1's view (landscape, not portrait).

So view controller views are not automatically resizing to fill the screen, and the presented view controller's supported orientations are not being honored.

So how are we supposed to do this now? Does it have to do with trait collections? With preferred content size? With setting the status bar orientation manually?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer to part one (launching into landscape) is unchanged from iOS 7: everything depends on the order of possible orientations in the Info.plist file. So, let's say that View Controller 1 says this:

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

Then we will coherently launch into landscape if a landscape orientation comes first in the Info.plist's supported interface orientations:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
    <string>UIInterfaceOrientationPortrait</string>
</array>

One noteworthy change in iOS 8 is that by default the status bar is hidden when we're in landscape. But you can prevent this, if desired, with the appropriate override:

-(BOOL)prefersStatusBarHidden {
    return NO;
}

That doesn't answer the second part of my question, which is how to force rotation when a view controller is presented. As I have explained in this answer, my sense is that this will become impossible in iOS 8. Your view controller and your views are expected to "adapt" — and so are you.

EDIT: It looks like in seed 4 the ability to force app rotation on view controller presentation/dismissal is returning!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...