即使它的风景,iPad模态视图控制器在纵向行动

时间:2012-03-06 11:59:49

标签: iphone objective-c ios ipad rotation

我在横向模式下呈现ModalViewController时遇到了一些困难。基本上它以正确的界面方向显示,只有当设备处于横向模式时才会旋转,但视图控制器就像是处于纵向模式一样。

我正在呈现这样的模态

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];

    LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
    [self.window.rootViewController presentModalViewController:lvc animated:NO];

    return YES;
}

在LoginViewController中

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}    

几次轮换后的日志结果:

2012-03-06 13:51:49.308 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
2012-03-06 13:51:49.313 OrientationTest[3132:207] {{0, 20}, {1024, 748}}
2012-03-06 13:51:49.314 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
2012-03-06 13:51:49.315 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
2012-03-06 13:51:51.647 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
2012-03-06 13:51:51.648 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
2012-03-06 13:51:53.481 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
2012-03-06 13:51:53.482 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
2012-03-06 13:51:53.897 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
2012-03-06 13:51:53.898 OrientationTest[3132:207] {{20, 0}, {748, 1024}}

基本上,loginViewController就像是处于纵向模式一样。我在这个视图上有两个文本字段,当我点击其中一个时,我想向上移动视图,因此键盘不会显示在文本字段上。为了做到这一点,我必须修改frame.origin.x而不是y因为轴被反转(视图就像是肖像),这导致了很多问题。

修改:

如果我将LoginViewController上的模态演示文稿样式更改为UIModalPresentationPageSheet,它会按预期工作,因此仅存在全屏模式的问题

第二次修改:

我已经将代码删除了基础知识。我甚至不再提供模态视图控制器只是将视图控制器初始化为根视图控制器,但仍然发生这种情况。

App代表:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

在我的视图控制器中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    NSLog(@"%@",NSStringFromCGRect( self.view.frame));
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

3 个答案:

答案 0 :(得分:10)

好的,所以我终于找到了一种方法来正常工作。看起来如果你只是创建一个新的视图控制器并表明它将始终被反转。我找到的修复程序是将视图控制器放在导航控制器中。

LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:lvc];
nvc.navigationBarHidden = YES;
lvc.modalPresentationStyle = UIModalPresentationFullScreen;
lvc.parentView = self.navController.view;
[self.window.rootViewController presentModalViewController:nvc animated:NO];

答案 1 :(得分:0)

x / y反转的帧是“特征”;)您缺少convertRect:fromView。在视图之间查看converting rects and points

在您的情况下,您需要将键盘框架的rect转换为视图控制器的主视图。没有看到你的键盘框架的东西,我不确切知道,但它可能是这样的:

[self.view convertRect:keyboardFrame fromView:keyboardView];

答案 2 :(得分:-3)

更改此

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}    

进入这个

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
    return YES;
}