【问题标题】:UISplitViewController stops auto-rotating after trying to use the UINavigationController inside of itUISplitViewController 在尝试使用其中的 UINavigationController 后停止自动旋转
【发布时间】:2023-03-22 22:02:01
【问题描述】:

我的 iPad 应用程序中的 UISplitViewController 遇到了一些问题。我正在尝试使用 UISplitView 内的 UINavigationController 制作一个简单的导航树。我已使用以下基本代码来执行此操作:

NavController.h

@interface NavController : NSObject {
    /* 
     * This is connected properly to the UINavigationController in the 
     * UISplitViewController through Interface Builder.
     */

    UINavigationController *navigationController;

 }

 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

 @end

NavController.m

#import "NavController.h"

@implementation NavController

@synthesize navigationController;

- (void) awakeFromNib {
    UIViewController *testController = [[UIViewController alloc] init];
    UITableView *tableView = [[UITableView alloc] init];

    [testController setView: tableView];

    [navigationController pushViewController: testViewController
                                    animated: YES];

}

@end

这段代码成功地将视图推送到导航控制器,我可以使用后退按钮导航回来,但是,我的问题在于,发生这种情况后,我的 UISplitViewController 不再自动旋转或从纵向位置。当我删除此代码(并且视图不会被推送)时,它会按预期工作。

我做错了什么,我是否以正确的方式解决这个问题?

【问题讨论】:

    标签: cocoa-touch ios uinavigationcontroller rotation uisplitviewcontroller


    【解决方案1】:

    这也让我发疯了。我做了几件事使它起作用,但我对我的解决方案不满意——1)我不太了解它,2)它看起来很老套。

    我将此添加到我的应用委托(我的 .m 文件):

    @interface UITabBarController (MyApp)
    @end
    
    @interface UINavigationController (MyApp)
    @end
    
    @implementation UITabBarController (MyApp) 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
    }
    @end
    
    @implementation UINavigationController (MyApp) 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
    }
    @end
    

    这在大多数情况下都有效。对于没有自动旋转的视图,我必须自己使用变换手动旋转视图。我做了类似的事情:

    - (void)deviceOrientationDidChangeWithAnimation:(BOOL)animated {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
        if (orientation == oldOrientation) {
            return;
        }
    
        if (animated) {
            CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:duration];
            [UIView setAnimationDidStopSelector:@selector(orientationChanged)];
        }
    
        [self sizeToFitOrientation:YES];
    
        if (animated) {
            [UIView commitAnimations];
        }
    
        oldOrientation = orientation;
    }
    
    - (CGAffineTransform)transformForOrientation {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            return CGAffineTransformMakeRotation(M_PI*1.5); // rotated CCW
        } else if (orientation == UIInterfaceOrientationLandscapeRight) { // CW
            return CGAffineTransformMakeRotation(M_PI/2);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { // CCW
            return CGAffineTransformMakeRotation(-M_PI);
        } else { // CW
            return CGAffineTransformIdentity;
        }
    }
    
    - (void)sizeToFitOrientation:(BOOL)transform {
        if (transform) {
            self.view.transform = CGAffineTransformIdentity;
        }
    
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
    
        CGFloat width = frame.size.width - 0 * 2;
        CGFloat height = frame.size.height - 0 * 2;
    
        UIInterfaceOrientation _orientation = [UIApplication sharedApplication].statusBarOrientation;
        if (UIInterfaceOrientationIsLandscape(_orientation)) {
            self.view.frame = CGRectMake(0, 0, height, width);
        } else {
            self.view.frame = CGRectMake(0, 0, width, height);
        }
        self.view.center = center;
    
        if (transform) {
            self.view.transform = [self transformForOrientation];
        }
    }
    

    希望这会有所帮助!如果有人能指出我犯过的错误(或我一直在做的坏事),我会很乐意学习。 :)

    【讨论】:

    • 有趣。我从没想过设置 UINavigationController 让它自动旋转。我会毫不犹豫地尝试一下。我希望 UISplitView 更易于使用...谢谢您的回答!
    • 今晚有机会尝试一下。我将我的 UINavigationController 子类化并添加了启用自动旋转所需的方法。它现在完美无缺!到目前为止,我还没有做任何手动旋转视图。非常感谢您的解决方案!
    • 太好了!我仍然不太明白它为什么会起作用,但我很高兴它起作用了:)
    • 谢谢!只是覆盖标签栏控制器对我有用!耶! =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2012-11-04
    相关资源
    最近更新 更多