iPad应用程序方向和重新定位问题

时间:2012-12-06 09:04:07

标签: ios xcode ipad orientation uimodalpresentationstyle

当我的UI流程如下时,我的应用程序中有一个奇怪的错误:

  1. mainViewController以纵向方向启动,然后显示一个LoginalController,其modalPresentationStyle设置为UIModalPresentationFullScreen

  2. 我将loginViewController转为横向模式,输入登录凭据(验证用户的方法在mainViewController中定义)。

  3. loginViewController被解除(来自mainViewController)并且一堆按钮被加载到屏幕上以指示它是主屏幕。

  4. 现在我的问题是按钮位置看起来好像应用程序处于纵向方向,即使在显示loginViewController时应用程序已切换为横向。

    此外,仅当modalPresentationStyle设置为UIModalPresentationFullScreen时才会发生这种情况!当我将它作为表单或页面表呈现时,一切正常(但是,我需要我的loginViewController才能显示FullScreen)。

    到目前为止,我已经尝试在loginViewController被解除等时手动调用shouldAutorotate方法,这解决了问题,但似乎是一种伪劣的解决方法而不是修复。

    我也尝试从loginViewController调用mainViewController的shouldAutorotate方法,但这没有任何改变。

    有关如何解决问题的任何想法?

2 个答案:

答案 0 :(得分:1)

在iOS 6中,

自动旋转已更改。在iOS 6中,不推荐使用UIViewController的shouldAutorotateToInterfaceOrientation:方法。取而代之的是,您应该使用supportedInterfaceOrientations:shouldAutorotate方法:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

模态ViewControllers 不再在iOS 6中获得轮换调用:在进行全屏演示的任何视图控制器上不再调用willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法超过自身 - 例如那些被调用的人:presentViewController:animated:completion:

This answer will further explain the rotation changes in iOS 6


[编辑] 完全不同的方式是注册轮换事件。优点是所有对象都可以注册,而不仅仅是UIViewController。这通常在视图加载时完成,并在视图消失时停止(在此处放置dealloc)。方向更改时调用选择器中的方法(此处为orientationChanged:):

- (void)viewDidLoad {
    [super viewDidLoad];
    // Start generating device rotations and register for them
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc {
    // Deregister and stop generating device rotations
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [super dealloc];
}

答案 1 :(得分:0)

check this out.... may be this'll help...


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     return (interfaceOrientation == UIInterfaceOrientationPortrait |interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown| interfaceOrientation == UIInterfaceOrientationLandscapeLeft | interfaceOrientation == UIInterfaceOrientationLandscapeRight);

}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

- (BOOL) shouldAutorotate
{
    return YES;
}