我正在尝试创建一个不随设备方向旋转的视图。 我想只是这一个视图不旋转,因为我有一个我不想旋转的UIToolbar。 这可能吗? 如果是这样,我该如何实现它? 我正在使用Swift。 此外,是否可以在UIToolbar上旋转带有设备方向的UIBarButtonItems? 如果是这样,我该如何实现呢? 重申:我希望其中的视图和工具栏不会随着方向旋转; 我希望工具栏上的按钮按方向旋转。 谢谢。
I am trying to create a view that doesn't rotate with device orientation. I want just this one view to not rotate because I have a UIToolbar that I don't want to rotate. Is this possible? If so, how do I implement it? I am using Swift. Also, is it possible to rotate the UIBarButtonItems with device orientation on the UIToolbar? If so, how do I implement that? Just to restate: I want the view and toolbar within it to not rotate with orientation; I want the buttons on the toolbar to rotate with orientation. Thanks.
最满意答案
根据View Controller编程指南
如果要暂时禁用自动旋转,请避免操作方向掩码来执行此操作。 而是覆盖初始视图控制器上的shouldAutorotate变量。 在执行任何自动旋转之前调用此方法。 如果返回NO,则抑制旋转。
因此,您需要子类化'UINavigationController',实现shouldAutorotate并在故事板中使用导航控制器类。
斯威夫特3
override var shouldAutorotate: Bool { let currentViewController = self.topViewController return !currentViewController.isKind(of: DetailViewController.classForCoder()) }I used the information from all of the answers here, as well as from this post overriding shouldAutorotate not working in Swift 3, to find the answer to my question. I created a MainNavigationController class within my MainViewController.swift file and then overrided shouldAutorotate like so:
import UIKit class MainNavigationController: UINavigationController { override var shouldAutorotate: Bool { return false } } class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } }The last step was to go into my Main.storyboard file, click on the navigation controller for the view, and change its class to MainNavigationController. Once I did that, it worked perfectly. Thanks to all the people who answered this question.
更多推荐
发布评论