有没有办法让一个视图不旋转?(Is there a way to have a single view not rotate?)

编程入门 行业动态 更新时间:2024-10-11 09:21:58
没有办法让一个视图不旋转?(Is there a way to have a single view not rotate?)

我正在尝试创建一个不随设备方向旋转的视图。 我想只是这一个视图不旋转,因为我有一个我不想旋转的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.

更多推荐

本文发布于:2023-08-07 04:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   视图   single   view   rotate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!