简体   繁体   English

如何设置第二个UIWindow的方向

[英]How do I set orientation of a second UIWindow

I have the following code in my main ViewController viewDidLoad function 我的主要ViewController viewDidLoad函数中有以下代码

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


nav = [[NavWithAutoRotateViewController alloc] initWithRootViewController:self];

[window addSubview:[nav view]];
[window makeKeyAndVisible];


[[self navigationController] setNavigationBarHidden:YES];

My ipad app is currently set to only work in landscape mode and I'm using this new window to show a quicklook document and allowing the nav bar to provide a back button and save options for the document. 我的ipad应用程序当前设置为仅在横向模式下使用,并且我正在使用此新窗口来显示快速查找文档,并允许导航栏提供后退按钮并保存该文档的选项。 Main problem is that the new UIWindow orientation doesn't match my main applications UIWindow. 主要问题是新的UIWindow方向与我的主要应用程序UIWindow不匹配。

I have a custom UINavigationController above called NavWithAutoRotateController and here is the code for that controller. 我上面有一个自定义UINavigationController,名为NavWithAutoRotateController,这是该控制器的代码。

-(id)init
{
    if(self)
    {
//        _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
//        _orientation = UIInterfaceOrientationLandscapeLeft;
    }


    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}


// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskLandscape;
}

I think I have a solution. 我想我有解决办法。 The problem seems to be in the assignment of UIViewController to the rootViewController in your extra UIWindow . 问题似乎rootViewController在额外的UIWindow rootViewController UIViewController分配给rootViewController

If you just assume that you can use the same view controller that your primary UIWindow is using, and then add things as subviews of the new UIWindow, there are orientation issues. 如果仅假设您可以使用与主UIWindow使用的视图控制器相同的视图控制器,然后将其添加为新UIWindow的子视图,则存在方向问题。

To solve this, I did the following: 为了解决这个问题,我做了以下工作:

UIWindow *newWindow = [[UIWindow alloc] initWithFrame: [UIApplication sharedApplication].keyWindow.frame];
newWindow.windowLevel = UIWindowLevelStatusBar+1;
// You can use a view controller instantiated from a xib or storyboard here if you want.
// Just don't use the view controller already set as a UIWindow's rootViewController.
UIViewController *newViewController = [[UIViewController alloc] init];
newWindow.rootViewController = newViewController;
[newWindow makeKeyAndVisible];
// Add something to the new UIWindow. Can use the new UIViewController's view:
[newViewController.view addSubview: myContentView];
// Or could add it as subview of UIWindow: - either works.
[newWindow addSubview: myContentView];

Doing it this way seems to have solved all the weird issues around rotation. 这样做似乎解决了围绕旋转的所有怪异问题。 Hope this helps. 希望这可以帮助。

Register for status bar frame change notification which is only called (afaik) when orientation changes and then define your new window's orientation.. 注册状态栏框架更改通知,该通知仅在方向更改时称为(afaik),然后定义新窗口的方向。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appWillChangeStatusBarFrameNotification:)
                                             name:UIApplicationWillChangeStatusBarFrameNotification
                                           object:nil];

I was having the same problem. 我遇到了同样的问题。 Orientation changes were being handled properly using the viewWillTransition method. 已使用viewWillTransition方法正确处理了方向更改。 But my problem was that in some edge case conditions, namely if my device was sitting at an odd angle, the custom UIWindow would initialize in portrait orientation, even though the rootViewController was in landscape. 但是我的问题是,在某些极端情况下,即如果我的设备坐在一个奇怪的角度,即使rootViewController是横向的,自定义UIWindow也会以纵向方向进行初始化。 And viewWillTransition isn't called because the device isn't rotated on initial load. 由于设备在初始加载时不会旋转,因此未调用viewWillTransition I found a really simple solution to my problem that worked in any situation I tested. 我找到了一个非常简单的解决方案,可以在我测试的任何情况下都可以使用。 First initialize your custom UIWindow without a frame. 首先初始化没有框架的自定义UIWindow。 Then set your frame. 然后设置框架。 And voila.. its orientation is what you'd expect. 瞧,它的方向就是您所期望的。

Swift 迅速

let customWindow = UIWindow()
customWindow.frame = CGRect(x: x, y: y, width: w, height: h)
customWindow.rootViewController = self //<your_viewController>
customWindow.windowLevel = .statusBar // or whatever level you need
customWindow.makeKeyAndVisible()

ObjC ObjC

UIWindow customWindow = [[UIWindow alloc] init];
customWindow.frame = CGRectMake(x, y, w, h);
customWindow.rootViewController = self;
customWindow.windowLevel = UIWindowLevelStatusBar;
[customWindow makeKeyAndVisible];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM