首次运行时iOS视图为空

时间:2013-02-27 03:14:12

标签: iphone ios uiview uiinterfaceorientation

问题是,当我第一次运行它时,我的应用程序不会在NSLog内显示日志。但是当我将我的方向改为风景时,文字出现了。顺便说一下,应用程序的默认视图是纵向的。

viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
}

但是,如果我在viewDidLoad:

中执行此操作
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    NSTimeInterval duration = 0.0;

    [self willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

它可以毫不费力地显示日志。

问题是,我是否以正确的方式显示日志?

我的willAnimateRotationToInterfaceOrientation:toInterfaceOrientation:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationPortrait:
                NSLog(@"iPhone/Portrait");
                break;

            case UIInterfaceOrientationLandscapeLeft:
                NSLog(@"iPhone/Landscape/Left");
                break;

            case UIInterfaceOrientationLandscapeRight:
                NSLog(@"iPhone/Landscape/Right");
                break;

            default:
                break;
        }
    }

    else {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationPortrait:
                NSLog(@"iPad/Portrait");
                break;

            case UIInterfaceOrientationPortraitUpsideDown:
                NSLog(@"iPad/Upside Down");
                break;

            case UIInterfaceOrientationLandscapeLeft:
                NSLog(@"iPad/Landscape/Left");
                break;

            case UIInterfaceOrientationLandscapeRight:
                NSLog(@"iPad/Landscape/Right");
                break;

            default:
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

斯科特罗斯的回答起到了作用:willAnimateRotationToInterfaceOrientation not called on popViewControllerAnimated

添加了updateLayoutForNewOrientation:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
            NSLog(@"Portrait");
        }

        else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            NSLog(@"Landscape");
        }
    }

    else {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
            NSLog(@"Portrait");
        }

        else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            NSLog(@"Landscape");
        }
    }
}

将我的willAnimateRotationToInterfaceOrientation:更改为

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self updateLayoutForNewOrientation:toInterfaceOrientation];
}

并将[self updateLayoutForNewOrientation:[self interfaceOrientation]];添加到viewDidLoad: