5

我的主 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 应用程序当前设置为仅在横向模式下工作,我正在使用这个新窗口来显示快速查看文档并允许导航栏提供后退按钮和文档的保存选项。主要问题是新的 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;
}
4

3 回答 3

4

我想我有一个解决办法。问题似乎UIViewController在于rootViewController您的 extra的分配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];

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

于 2015-05-13T09:33:23.973 回答
1

我遇到了同样的问题。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()

对象

UIWindow customWindow = [[UIWindow alloc] init];
customWindow.frame = CGRectMake(x, y, w, h);
customWindow.rootViewController = self;
customWindow.windowLevel = UIWindowLevelStatusBar;
[customWindow makeKeyAndVisible];
于 2019-07-29T19:25:35.443 回答
0

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appWillChangeStatusBarFrameNotification:)
                                             name:UIApplicationWillChangeStatusBarFrameNotification
                                           object:nil];
于 2013-08-16T21:03:36.560 回答