ios - 调用 shouldAutorotateToInterfaceOrientation 但锁定 View 不旋转

标签 ios

我想创建一个 UIVIew,当我调用 shouldAutorotateToInterfaceOrientation 时不会旋转,而其他 subview 会旋转。

我想保留 shouldAutorotateToInterfaceOrientation 支持,而不使用通知。

谢谢

最佳答案

请务必准确定义设备旋转时“不旋转” View 的含义。旋转可能意味着几件事,具体取决于您所指的坐标系。一个更好的思考方式很简单,你希望你的 View 在每个设备方向上看起来像什么。

提醒一下,系统会将 shouldAutorotateTo... 发送到您的 View Controller 。您不会自己调用它。它不会引起旋转。它让系统询问您的 View Controller 它支持什么方向。

您的 VC 应该对它支持的所有方向回答"is"。受支持的方向是 View 响应设备方向更改而更改布局的方向,因此如果给定方向发生任何布局更改,则 shouldAutorotateTo 的答案可能是 YES。

为给定的界面方向改变 subview 布局主要是你的责任。 View 有一个 autoresizingMask,它是一个位向量,描述了一些相对于其父级的大小和定位选项,这通常就足够了。完全控制方向更改布局的方法是实现 willAnimateRotationToInterfaceOrientation。

例如,这里有一个相当宽松的 shouldAutorotate,启用除一个方向之外的所有方向...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

下面是如何控制 subview 在旋转时的布局...

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    UIView *testView = [self.view viewWithTag:16];

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        // change frames here to make the ui appear according to your spec
        // including however you define "not rotating" for each view
        self.subviewA.frame = .....
        self.subviewB.frame = .....                
    } else {
        self.subviewA.frame = .....
        self.subviewB.frame = .....
    }
}

关于ios - 调用 shouldAutorotateToInterfaceOrientation 但锁定 View 不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11384207/

相关文章:

ios - 在 Xcode 9 Beta 5 中向 NSMutableArray 添加元素时出错

ios - sip header 联系私有(private)ip。可以吗?

ios - 播放部分下载的 mp4 视频

ios - 如何使用页面启用的 UIScrollView,内容大小大于 scrollView 宽度

ios - 如何为segue名称创建变量?

jquery - 移动设备上的 ScrollTop

ios - 从核心数据 swift 4 中获取关系

iphone - iCloud 基础知识和代码示例

ios - 带有 [NSNull null] 的 NSMutableArray 与 NSPointerArray

ios - 使用自动布局在父 View 中均匀分布 View