如何设置第二个UIWindow的方向

时间:2013-08-15 15:44:58

标签: ios objective-c uiwindow

我的主ViewController viewDidLoad函数

中有以下代码
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


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

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


[[self navigationController] setNavigationBarHidden:YES];

我的ipad应用程序当前设置为仅在横向模式下工作,我正在使用此新窗口显示quicklook文档,并允许导航栏提供后退按钮并保存文档选项。主要问题是新的UIWindow方向与我的主应用程序UIWindow不匹配。

我上面有一个名为NavWithAutoRotateController的自定义UINavigationController,这是该控制器的代码。

-(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;
}

3 个答案:

答案 0 :(得分:4)

我想我有一个解决方案。问题似乎在于将UIViewController分配给额外rootViewController中的UIWindow

如果您只是假设您可以使用主UIWindow使用的相同视图控制器,然后将内容添加为新UIWindow的子视图,则存在方向问题。

为了解决这个问题,我做了以下几点:

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];

这样做似乎解决了围绕旋转的所有奇怪问题。 希望这会有所帮助。

答案 1 :(得分:1)

注册状态栏框架更改通知,仅在方向更改时调用(afaik),然后定义新窗口的方向..

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

答案 2 :(得分:0)

我遇到了同样的问题。使用viewWillTransition方法已正确处理了方向更改。但是我的问题是,在某些极端情况下,即如果我的设备坐在一个奇怪的角度,即使rootViewController是横向的,自定义UIWindow也会以纵向方向进行初始化。并且viewWillTransition之所以没有被调用是因为设备在初始加载时不会旋转。我找到了一个非常简单的解决方案,可以在我测试的任何情况下都可以使用。 首先初始化没有框架的自定义UIWindow。然后设置框架。瞧,它的方向就是您所期望的。

迅速

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

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