仅允许一个控制器旋转

时间:2022-05-01 21:29:31

I need to lock all controllers from auto rotation except one. It must rotates both portrait and landscape. I have read this topic and tried this solutions

我需要锁定除自动旋转以外的所有控制器。它必须旋转纵向和横向。我已阅读此主题并尝试了此解决方案

let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

but I had no luck, it didnt work. Maybe this is because I use navigation controllers, I saw some mentions of them in previous link but I didnt understand approach because author allowed orientation modes in Xcode preferences and then duplicated them in code.

但我没有运气,它没有用。也许这是因为我使用导航控制器,我在之前的链接中看到了一些提及但我不理解的方法,因为作者允许Xcode首选项中的方向模式,然后在代码中复制它们。

Maybe some one can help with advice ?

也许有人可以提供建议吗?

2 个解决方案

#1


0  

The current method to handle rotation is viewControllerWillTransitionToSize:withTransitionCoordinator:. Documentation is at this link: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:

处理旋转的当前方法是viewControllerWillTransitionToSize:withTransitionCoordinator:。文档位于以下链接:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:

It's a little bit different because you're working with screen sizes not orientations. This is the new way of dealing with sizes and transitions. Think of it like a responsive layout instead of distinct rotation values. To put it a different way, you're not designing for "landscape orientation" anymore, but for a screen thats wider than it is tall. It's a subtle but important difference.

它有点不同,因为你正在使用屏幕尺寸而不是方向。这是处理大小和过渡的新方法。可以把它想象成一个响应式布局而不是不同的旋转值。换句话说,你不再为“横向”设计,而是为了比它更高的屏幕。这是一个微妙但重要的区别。

You could implement this method in different ways for different view controllers. If you're using a navigation controller and want to affect child views differently, first I'd say thats terrible UX most likely. But if you still want to do it, you could handle rotation in your Nav Controller.

您可以以不同的方式为不同的视图控制器实现此方法。如果您正在使用导航控制器并希望以不同方式影响子视图,那么首先我会说这种可能性很糟糕。但是如果您仍想这样做,您可以在Nav Controller中处理旋转。

You should not set the device orientation like that.

您不应该像这样设置设备方向。

#2


0  

you can try this method. Paste this method on the view controller to make the device support portrait and landscape except upside down orientation.

你可以尝试这种方法。将此方法粘贴到视图控制器上,使设备支持纵向和横向,但颠倒方向除外。

func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMaskAllButUpsideDown
}

But as chris suggested, you should use viewControllerWillTransitionToSize and handle everything regarding the orientations there.

但正如克里斯所建议的那样,你应该使用viewControllerWillTransitionToSize来处理那里有关方向的一切。

#1


0  

The current method to handle rotation is viewControllerWillTransitionToSize:withTransitionCoordinator:. Documentation is at this link: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:

处理旋转的当前方法是viewControllerWillTransitionToSize:withTransitionCoordinator:。文档位于以下链接:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:

It's a little bit different because you're working with screen sizes not orientations. This is the new way of dealing with sizes and transitions. Think of it like a responsive layout instead of distinct rotation values. To put it a different way, you're not designing for "landscape orientation" anymore, but for a screen thats wider than it is tall. It's a subtle but important difference.

它有点不同,因为你正在使用屏幕尺寸而不是方向。这是处理大小和过渡的新方法。可以把它想象成一个响应式布局而不是不同的旋转值。换句话说,你不再为“横向”设计,而是为了比它更高的屏幕。这是一个微妙但重要的区别。

You could implement this method in different ways for different view controllers. If you're using a navigation controller and want to affect child views differently, first I'd say thats terrible UX most likely. But if you still want to do it, you could handle rotation in your Nav Controller.

您可以以不同的方式为不同的视图控制器实现此方法。如果您正在使用导航控制器并希望以不同方式影响子视图,那么首先我会说这种可能性很糟糕。但是如果您仍想这样做,您可以在Nav Controller中处理旋转。

You should not set the device orientation like that.

您不应该像这样设置设备方向。

#2


0  

you can try this method. Paste this method on the view controller to make the device support portrait and landscape except upside down orientation.

你可以尝试这种方法。将此方法粘贴到视图控制器上,使设备支持纵向和横向,但颠倒方向除外。

func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMaskAllButUpsideDown
}

But as chris suggested, you should use viewControllerWillTransitionToSize and handle everything regarding the orientations there.

但正如克里斯所建议的那样,你应该使用viewControllerWillTransitionToSize来处理那里有关方向的一切。