OGeek|极客世界-中国程序员成长平台

标题: ios - iOS6下多个xib旋转的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:05
标题: ios - iOS6下多个xib旋转的问题

我需要为纵向和横向使用不同的 xib 文件。我没有使用自动布局,但我使用的是 iOS6。 (如果您关心原因,请参阅 my previous question。)

我正在关注亚当对 this question 的回答用 amergin 的 initWithNib 名字技巧修改,用我自己的 iPhone/iPad 需要修改。这是我的代码:

-(void)willRotateToInterfaceOrientationUIInterfaceOrientation)toInterfaceOrientation durationNSTimeInterval)duration
{    
    [[NSBundle mainBundle] loadNibNamed:[self xibNameForDeviceAndRotation:toInterfaceOrientation]
                                  owner: self
                                options: nil];
    [self viewDidLoad];
}

- (NSString *) xibNameForDeviceAndRotationUIInterfaceOrientation)toInterfaceOrientation
{
    NSString *xibName ;
    NSString *deviceName ;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        deviceName = @"iPad";
    } else {
        deviceName = @"iPhone";
    }

    if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
    {
        xibName = [NSString stringWithFormat"%@-Landscape", NSStringFromClass([self class])];
        if([[NSBundle mainBundle] pathForResource:xibName ofType"nib"] != nil) {
            return xibName;
        } else {
            xibName = [NSString stringWithFormat"%@_%@-Landscape", NSStringFromClass([self class]), deviceName];
            if([[NSBundle mainBundle] pathForResource:xibName ofType"nib"] != nil) {
                return xibName;
            } else {
                NSAssert(FALSE, @"Missing xib");
                return nil;
            }
        }

    } else {
        xibName = [NSString stringWithFormat"%@", NSStringFromClass([self class])];
        if([[NSBundle mainBundle] pathForResource:xibName ofType"nib"] != nil) {
            return xibName;
        } else {
            xibName = [NSString stringWithFormat"%@_%@", NSStringFromClass([self class]), deviceName];
            if([[NSBundle mainBundle] pathForResource:xibName ofType"nib"] != nil) {
                return xibName;
            } else {
                NSAssert(FALSE, @"Missing xib");
                return nil;
            }
        }
    }
}

当然我在做:

- (BOOL) shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

在我的 View Controller 中:

- (NSUInteger)applicationUIApplication *)application supportedInterfaceOrientationsForWindowUIWindow *)window
{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}

在我的委托(delegate)中。

我有两个可能相关的问题。第一,简单的。我不倒转。我为 iPad 和 iPhone 在 xcode 中打开了所有正确的位。这可能是一个单独的问题,也可能是我的问题的核心。

真正的问题是,当我旋转到横向模式时,我的 xib 被替换,但 View 偏离了 90 度。

这是我的 2 xib 的样子。 (我给它们涂上了花哨的颜色,所以你可以看到它们是不同的。)

enter image description hereenter image description here

当我运行它时(最初在横向模式下),您可以看到横向 xib 是正确的。

enter image description here

当我旋转到纵向时它也是正确的

enter image description here

但是当我旋转回横向时,xib 被替换,但 View 偏离了 90 度。

enter image description here

这里有什么问题?



Best Answer-推荐答案


我可能一直在走与保罗·塞尚去年相同的道路。不确定他是否尝试过这个,但我解决了最初的问题(在这个问题中说明),只是让我的根 Controller 成为导航 Controller 而不是我的 View Controller 类。由于我使用的是“空项目”模板和 XIB 文件,这意味着更改正常:

self.viewController = [[ViewController alloc] initWithNibName"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;

在 AppDelegate.m 中,改为:

self.viewController = [[ViewController alloc] initWithNibName"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navigationController.navigationBar.hidden = YES;
self.window.rootViewController = navigationController;

也就是说,我刚刚创建了一个通用 UINavigationController 并将其设置为 Root View Controller 。

我不确定这是否会导致其他问题,并且可能有一种方法可以弄清楚(也许您需要源代码)UINavigationController 做了哪些 UIViewController 没有。可以像在正确的地方调用一个额外的 setNeedsLayout 类型一样简单。如果我弄明白了,我会为 future 的读者编辑这个答案。

归功于 Sakti 对 Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape? 的评论我第一次阅读它们时不应该忽略它们:

i added the view controller to navigation controller and presented it which made it work as intended

编辑:在示例代码中添加了额外的行以隐藏导航栏,因为大多数关注此问题的人都不希望这样做。

关于ios - iOS6下多个xib旋转的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435397/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4