【问题标题】:How would i load a xib when device orientation is changed in xcode?在 xcode 中更改设备方向时如何加载 xib?
【发布时间】:2010-08-31 03:20:59
【问题描述】:

当设备的方向改变时,我将如何加载新的 xib?即当设备变成横向时加载横向的xib,反之亦然。我将如何自动检测方向变化并从那里进行调整?

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    显然我所要做的就是创建一个基于视图的应用程序将 shouldAutoRotateToInterfaceOrientaion 设置为 YES,并使用单独的 xib 文件创建两个 uiviewcontroller 子类,然后将其添加到应用程序视图控制器:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    并将此方法也添加到应用视图控制器中:

    -(void)orientationChanged {
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
            [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
        }
        else {
            [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
        }
    
    }
    

    就是这样,当设备旋转时,xib 会自动加载到正确的方向。

    编辑:您还需要以编程方式添加两个视图控制器(@property 和 @synthesize 它们也),以及 IB 中 appviewcontroller.xib 中的两个视图控制器,每个视图控制器的类名对应于您创建的每个 xib 的视图控制器子类。然后添加一个视图,将其连接到文件的所有者,并将 IB 中的插座连接到您在 ib 中创建的视图控制器,这应该是全部

    【讨论】:

    • 这非常适合 iPhone。 iPad 需要额外的步骤:#define DegreesToRadians(x) ((x) * M_PI / 180.0); (self.view.transform = CGAffineTransformMakeRotation(DegreesToRadians(180)) 至少在我的情况下。
    猜你喜欢
    • 2011-04-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多