我正在学习在 Xcode 中编写 View ,而不是使用 .storyboard
。
我不希望 View 在旋转时旋转。
到目前为止我有这个。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .white
let imageView = UIImageView(image: #imageLiteral(resourceName: "facebook_logo.png"))
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 300).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
最佳答案
我认为上面@oaccamsrazer 提供的链接是您需要的。为了提供帮助,我实现了一个小示例项目。
有两个 View Controller ,由一个 segue 链接(并且都包装在一个 UINavigationController 中)。
在 AppDelegate 中,您需要以下内容:
var restrictRotation:UIInterfaceOrientationMask = .all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}
和 viewWillAppear(因为它在我们遍历堆栈时被调用,所以比使用 viewDidLoad 更好)我们
override func viewWillAppear(_ animated: Bool) {
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all
}
在 viewDidLoad 中(你也可以使用 viewWillAppear)
override func viewDidLoad() {
super.viewDidLoad()
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
}
只使用 Storyboard或代码没有区别,实现仍然是一样的。
关于ios - Xcode : Disable rotation for a view in the ViewController. swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55622364/