Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Status bar is Landscape, but [[UIApplication sharedApplication] statusBarOrientation] returns portrait

This problem appears to be intermittent, but I am not sure why exactly. When running my application on the device (iPad), I have some code to load a scroll view with some image views according to the current device orientation. Even though the device is landscape before loading, the views are being loaded as if it were portrait.

The orientation is found by calling [[UIApplication sharedApplication] statusBarOrientation].

The views are set up to adjust their positions when the device is rotated, and indeed, rotating to portrait and then back to landscape returns them to the correct landscape positions. Is it the case that all applications start off in portrait and soon change to landscape if required? Am I trying to check the orientation too soon (during the init of the first view controller to be loaded)?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

OK Fixed.

Using UINavigationController, when I popToViewController:animated: from a landscape view to a portrait view, the destination view appears correct but the status bar and also the UIKeyboard keeps the landscape configuration, making a real mess.

Working around After thousands of recommendations about statusBarOrientation and references read... https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html

"The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0. This makes the caller responsible for ensuring that the status bar orientation is consistent." (thanks to Vytis in here)

statusBarOrientation only works if supportedInterfaceOrientations returns 0, so... that give us a guess.

If statusBarOrientation is not as expected, one zero return will do it (if always return 0, the view wont rotate, so:

// if deviceOrientation is A (so I expect statusbarOrientation A
// but statusbarOrientation is B
// return 0
// otherwise 
// return user interface orientation for A

- (NSUInteger)supportedInterfaceOrientations {
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
    if(deviceOrientation == UIDeviceOrientationPortrait || deviceOrientation == UIDeviceOrientationPortraitUpsideDown){
        if(statusBarOrientation != UIInterfaceOrientationPortrait ||statusBarOrientation != UIInterfaceOrientationPortraitUpsideDown){
             return 0;
        }
    }
    // otherwise
    return UIInterfaceOrientationMaskPortrait;
}

Now, in viewDidAppear (believe me, I use this call even when the keyboard notification is recived:

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;

more than 48 labor hrs in this. Hope this helps a while, thanks to all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...