本文介绍了防止AlertView自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的启动页面仅使用以下代码设置为 portrait

The launch page of my app is set to portrait only with this little bit of code:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait ;
}

当应用程序启动 UIAlertView 出现用于输入用户名和密码。
显示它的方法是从 viewWillAppear 调用的。

When the app launches a UIAlertView appears for username and password entry.The method to display it is called from viewWillAppear.

这适用于 iOS6 但是从 iOS7 开始,如果我将设备切换为横向,主视图仍保持纵向,但警报视图和键盘旋转为横向。
另一个奇怪的怪癖是当我切换回肖像时,只有键盘切换回来(以截断的形式),让警报以横向模式冻结:

This worked fine for iOS6 but since iOS7, if I switch the device to landscape, the main view remains in portrait but the the alert view and keyboard rotate to landscape.A further bizarre quirk is that when I switch back to portrait, only the keyboard switches back (in truncated form), leaving the alert frozen in landscape mode:

任何人都可以告诉我如何防止这种情况?

Can anyone tell me how to prevent this?

-EDIT -

调用自动旋转代码在一个单独的类别中:

The autorotate code is called in a separate category:

@implementation UINavigationController (Orientation)

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) return NO;
    else return YES;
}

@end

-EDIT 2 -

我也尝试在 UIAlertView上创建一个类别但它永远不会被调用:

I've also tried creating a Category on UIAlertView but it's never called:

@implementation UIAlertView (Orientation)

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-EDIT 3 -

我不确定这是多么相关,但这里是显示提醒的代码:

I'm not sure how relevant this is but here's the code for showing the alert:

- (void)alertWithMessage:(NSString *)theMessage
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login"
                                                    message:theMessage
                                                   delegate:self
                                          cancelButtonTitle:@"Login"
                                          otherButtonTitles: nil];

    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    UITextField *nameField = [alert textFieldAtIndex:0];
    [alert show];
}


推荐答案

尝试在<$中执行此操作C $ C> viewDidAppear:。我之前看到过这种奇怪的行为,因为视图布局尚未完全定义。在 viewDidAppear:中,所有内容都已设置并布局,因此此时不会出现任何问题。

Try doing this in viewDidAppear:. I've seen weird behavior like this before because view layouts are not entirely defined yet. In viewDidAppear:, everything is set and laid out, so there shouldn't be any problems at that point.

这篇关于防止AlertView自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:13
查看更多