ios - 根据方向更改 iOS 更改 View

标签 ios ipad

所以我在几个 View Controller 中使用了以下代码来实现一些自定义对象,这些对象根据方向的变化以不同的方式绘制。

出于某种原因,以下两种方法的这种特定实现不起作用:

- (void)awakeFromNib
{
    isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:[UIDevice currentDevice]];
}



- (void)orientationChanged:(NSNotification *)notification
{
    NSLog(@"Are we changing orientations?");
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        isShowingLandscapeView = YES;
        NSLog(@"Landscape!");

        /*

         // Landscape orientation
         [txtLoginID setFrame:CGRectMake(489, 200, 145, 30)];
         [txtPassword setFrame:CGRectMake(489, 235, 145, 30)];
         [loginLabel setFrame:CGRectMake(378, 203, 69, 21)];
         [pwdLabel setFrame:CGRectMake(378, 238, 80, 21)];
         loginButton.frame = CGRectMake(561, 273, 73, 30);

         // Set iPad landscape background
         background.frame = CGRectMake(0, 0, 1024, 748);
         [background setContentMode:UIViewContentModeScaleToFill];
         [background setImage:[UIImage imageNamed:@"ipadLandscape.jpg"]];
         [self.view addSubview:background];
         */
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             isShowingLandscapeView)
    {
        isShowingLandscapeView = NO;
        NSLog(@"Portrait!");

        /*
         // Portrait orientation
         txtLoginID.frame = CGRectMake(367, 254, 145, 30);
         txtPassword.frame = CGRectMake(367, 289, 145, 30);
         loginLabel.frame = CGRectMake(256, 257, 69, 21);
         pwdLabel.frame = CGRectMake(256, 292, 80, 21);
         loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         loginButton.frame = CGRectMake(439, 327, 73, 30);

         // Set iPad portrait background
         background.frame = CGRectMake(0, 0, 768, 1004);
         [background setContentMode:UIViewContentModeScaleToFill];
         [background setImage:[UIImage imageNamed:@"ipadPortrait.jpg"]];
         [self.view addSubview:background];
         */
    }
}

关于为什么这不起作用的任何想法?我的日志语句似乎没有产生任何东西。

最佳答案

对于滞后,也许试试这个方法:

- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration

关于ios - 根据方向更改 iOS 更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979254/

相关文章:

ios - 添加 .xib , .h & 。 m 到现有的 Storyboard。

ios - 一个应用程序可以被 Itunesconnect 中同一团队 ID 中的另一个应用程序替换吗?

ios - 隐藏 iOS 键盘,但允许用户移动光标

iphone - 我如何在方法中使用此值 - Objective C

php - 如何交换php变量字符

ios - 使用手势从底部滑动 View Controller

ios - 为什么 UIImagePickerControllerDelegate 方法没有触发?

iphone - 在 XCode 4 中的多个 Storyboard之间转换

xcode - putpkt:写入失败,管道损坏

ipad - 从适用于 iPad 的 Phonegap 应用程序中安装 OTA plist 应用程序