gpt4 book ai didi

ios - 在导航 Controller 中禁用 View Controller 的旋转

转载 作者:可可西里 更新时间:2023-11-01 04:06:00 24 4
gpt4 key购买 nike

首先,这不是重复的。我已经在 SO 上查看了与此相关的所有问题,但没有一个对我有用。希望这只是因为我是 iOS 开发的新手,但我怀疑这对我来说是不可能的。我附上了一张圈出 View Controller 的图片,我想为其禁用旋转。

我已经尝试过:子类化我想禁用旋转的 View Controller ,并通过将其设置为 NO 来使用 shouldAutoRotate 方法。显然这是行不通的,因为导航 Controller 决定了它的 View Controller 是否可以旋转。因此,我对 UINavigationController 进行了子类化,并在 Storyboard 中使用了此类而不是默认的导航 Controller 。在本类(class)中,我将 shouldAutoRotate 设置为 NO。它仍然不起作用。真的不知道我做错了什么。

当我使用我的 View Controller 类扩展 Root View Controller 并将 shouldAutoRotate 设置为 NO 时,它会禁用整个应用程序的旋转...。这不是我想要的。我只想为图片中圈出的 View Controller 禁用旋转。

enter image description here

提前致谢!

最佳答案

添加你的 AppDelegate.h

@property (nonatomic , assign) bool blockRotation;

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application       supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.blockRotation) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAll;
}

在要禁用旋转的 View Controller 中

- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate* shared=[UIApplication sharedApplication].delegate;
shared.blockRotation=YES;
}

-(void)viewWillDisappear:(BOOL)animated{
AppDelegate* shared=[UIApplication sharedApplication].delegate;
shared.blockRotation=NO;

}

Swift 4.2 及更高版本:

在你的 AppDelegate.swift 中:

var blockRotation = false

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if blockRotation {
return .portrait
}
return .all
}

在你的 View Controller 中:

override func viewDidLoad() {
super.viewDidLoad()
AppDelegate.shared.blockRotation = true
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppDelegate.shared.blockRotation = false
}

关于ios - 在导航 Controller 中禁用 View Controller 的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com