如何在willRotateToInterfaceOrientation中获取正确的NavigationController帧大小?

S.J*_*Lim 1 objective-c

我正在尝试获得正确的内容框架大小,该大小考虑了导航栏的高度大小.
但是如果在willRotateTo~方法中使用NSLog输出帧大小,则显示较早的帧大小.
但是它在didRotateFrom~方法中显示正确的帧大小.
如何在willRotateTo~方法中获得类似didRotateFrom~大小的帧大小?

以下是AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    ViewController *rootView = [[[ViewController alloc] init] autorelease];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[UINavigationController alloc] initWithRootViewController:rootView] autorelease];

    } else {
        self.viewController = [[[UINavigationController alloc] initWithRootViewController:rootView] autorelease];
    }

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
Run Code Online (Sandbox Code Playgroud)


接下来是ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"willRotate size is width : %f  height : %f", self.view.frame.size.width, self.view.frame.size.height);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    NSLog(@"didRotate size is width : %f  height : %f", self.view.frame.size.width, self.view.frame.size.height);
}
Run Code Online (Sandbox Code Playgroud)


以下是旋转到横向时的结果.

在此输入图像描述

/Users/nice7285/Library/Caches/appCode10/DerivedData/testView-d007adbb/Build/Products/Debug-iphonesimulator/testView.app/testView
Simulator session started with process 3896
2012-10-11 04:28:59.719 testView[3896:11303] willRotate size is width : 320.000000  height : 416.000000
2012-10-11 04:29:00.025 testView[3896:11303] didRotate size is width : 480.000000  height : 268.000000
Run Code Online (Sandbox Code Playgroud)

nen*_*hev 7

接受的答案并没有真正解决您所面临的问题.如果在didRotateFromInterfaceOrientation中执行UI更改,则UI会在屏幕执行其旋转动画后发生更改.这最终看起来非常丑陋和突然.

您应该使用willAnimateRotationToInterfaceOrientation,它在旋转发生后调用,边界已重新计算,并允许您为动画设置UI控件.这将让您执行从一个方向到另一个方向的平滑过渡.

  • 是的,这是正确的答案.我第一次使用**didRotateFromInterfaceOrientation**,似乎工作正常.但谎言@nenchev它显示了一些丑陋的变换动画.我用**willAnimateRotationToInterfaceOrientation**现在它的工作就像魅力一样.谢谢. (2认同)