0

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;
        }
    }
}
4

1 回答 1

0

Scott Roth 的回答成功了: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:

于 2013-02-27T05:15:09.460 回答