UIWindow with wrong size when using landscape orientation

后端 未结 6 882
南旧
南旧 2020-12-08 21:27

I have an empty application and there is no storyboard or xib involved. I want to have a hidden status bar and support only landscape orientation. Again, I wan\'t to make th

相关标签:
6条回答
  • 2020-12-08 22:08

    I had a similar problem, for a portrait-only app.

    I fixed the problem by setting status bar orientation BEFORE instantiate the UIWindow

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Init my stuff
        // ...
    
        // Prepare window
        [application setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; // prevent start orientation bug
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    In your case, you should use UIInterfaceOrienationLandscapeLeft (or Right) in the setStatusBarOrientation:animated: method.

    Hope it helps you.

    0 讨论(0)
  • 2020-12-08 22:12

    None of the solutions posted here or elsewhere worked for me.

    However, I found that this issue apparently does not occur with Storyboards, so an alternative solution is to move away from xibs. (This fact sadly also makes it unlikely that Apple will take the problem seriously.)

    0 讨论(0)
  • 2020-12-08 22:19

    The problem is solved when adding a Launch Screen, which you can only do by adding an extra property to the info.plist

    had this problem myself, i'm not sure if you can add it through code though, i only managed to make it work with info.plist + Launch Screen xib file

    <key>UILaunchStoryboardName</key>
    <string>Launch Screen</string>
    

    Actually i don't think you have to add a xib file, if just the key (with any value) is available in the plist it should work.

    0 讨论(0)
  • 2020-12-08 22:21

    When you run landscape app from portrait mode UIScreen has portrait bounds in iOS 8 (only if you haven't this app in app switch panel, as iOS 8 makes some cache). Even displaying window with makeKeyAndVisible doesn't change it's frame. But it changes [UIScreen mainScreen].bounds according to AppViewController avaliable orientation.

    #import "AppDelegate.h"
    #import "AppViewController.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // Portrait bounds at this point
      self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
      self.window.backgroundColor = [UIColor cyanColor];
      self.window.rootViewController = [[AppViewController alloc] init];
      [self.window makeKeyAndVisible];
      return YES;
    }
    
    @end
    

    So let's change window's frame after [self.window makeKeyAndVisible]

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [UIWindow new];
        self.window.backgroundColor = [UIColor cyanColor];
        self.window.rootViewController = [[AppViewController alloc] init];
        [self.window makeKeyAndVisible];
    
        // Here it is
        self.window.frame = [UIScreen mainScreen].bounds;
        return YES;
    }
    

    I think that it is iOS 8 bug.

    0 讨论(0)
  • 2020-12-08 22:29

    You can rotate UIWindow by adding single line only. You can set the rootController for your UIWindow. e.g:

    fileprivate(set) var bottonOverlayWindow = UIWindow()
    
    self.bottonOverlayWindow.rootViewController = self; 
    

    // 'self' will the ViewController on which you had added UIWindow view. So whenever you ViewController change the orientation, your window view also change it's orientation.

    Let me know if you face any issue.

    0 讨论(0)
  • 2020-12-08 22:33

    Personally, none of the solution presented above worked. I finally set "hidden" to YES for the window in my main xib, as first suggested here: unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow

    0 讨论(0)
提交回复
热议问题