Status bar is Landscape, but [[UIApplication share

2019-01-21 15:34发布

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)?

5条回答
来,给爷笑一个
2楼-- · 2019-01-21 15:40

Did you make sure you have all these in info.plist ?

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
查看更多
够拽才男人
3楼-- · 2019-01-21 15:47

If you are having this issue at launch time, you need to use UIDevice to get the device orientation, since statusBarOrientation will always be portrait until after application:didFinishLaunchingWithOptions:. The only caveat is that you need to enable device orientation notifications prior or asking UIDevice for the orientation.

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
查看更多
Anthone
4楼-- · 2019-01-21 15:49

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.

查看更多
SAY GOODBYE
5楼-- · 2019-01-21 15:56

If you're subclassing UIWindow or the status bar you'll see this because that's the ipad device's native orientation. The UIWindow translates the orientation and coordinates into what we're used to. After you makeAndKeyVisible, your device and interface orientation in view controllers should be as expected. You wouldn't by chance be using MTStatusBarOverlay would you? I went through the same thing and it came down to the order of instatiation.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-21 16:01

Just in case somebody else runs into this, I see a lot of apps that have similar issues in iOS5, actually on iPhone.

It might be a bug in iOS 5 or just an interference with a common behavior...

I use a custom root view controller class (a UITabBarController subclass, no idea whether that matters) and in that class I've overridden "shouldAutorotateToInterfaceOrientation" to only start rotating after my initial screen setup is done (doing otherwise messed some things up).

What I do now is I re-use this class to also set the statusBarOrientation manually to portrait before I allow rotations and to whatever the UI rotates to afterwards.

[[UIApplication sharedApplication] setStatusBarOrientation:toInterfaceOrientation animated:YES];

I believe this could fix THIS issue, too, even if the cause might be unrelated.

查看更多
登录 后发表回答