supportedInterfaceOrientations无法正常工作 [英] supportedInterfaceOrientations not working
问题描述
我想要一些我的ViewControllers景观和一些肖像,所以这就是我所做的:
I want some of my ViewControllers landscape and some portrait so this is what I did:
我启用了横向模式:
接下来,我将这些代码行添加到我希望成为肖像控制器的视图中:
Next I added these lines of code to the view controllers I wanted to be Portrait:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
-(BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
然而,当我旋转它们时,它们仍然会进入横向模式。我如何修复那个?
However when I rotate them they still go to landscape mode.How can I fix that?
推荐答案
我是这样做的。
创建一个UINavigationController父类。
你在里面
UINavigationController(父)覆盖这些方法,如:
Here is how I do it.
Make a UINavigationController parent class.
inside you UINavigationController (parent) override these methods like:
- (NSUInteger)supportedInterfaceOrientations
{
if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientationsForThisContorller)])
{
return(NSInteger)[self.topViewController performSelector:@selector(supportedInterfaceOrientationsForThisContorller) withObject:nil];
}
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
if([self.visibleViewController respondsToSelector:@selector(shouldAutorotateNow)])
{
BOOL autoRotate = (BOOL)[self.visibleViewController
performSelector:@selector(shouldAutorotateNow)
withObject:nil];
return autoRotate;
}
return NO;
}
现在你的NavigationController应该是UINavigationContorller父类的子类
Now your NavigationController should be a subclass of the UINavigationContorller parent
Swift 3:
在UINavigationController子类中执行此操作
Inside your UINavigationController subclass do this
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return self.topViewController?.supportedInterfaceOrientations ?? .all
}
}
override var shouldAutorotate: Bool {
return self.topViewController?.shouldAutorotate ?? false
}
更新
来自Matt's如果您不想进行子类化,请回答:
:使你的viewController成为 viewDidLoad
first: make your viewController a delegate of the navigationController in viewDidLoad
self.navigationController?.delegate = self
然后声明UIViewController扩展以响应委托方法,如下所示:
Then declare UIViewController extension to respond to the delegate method as shown below:
extension UIViewController: UINavigationControllerDelegate {
public func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return navigationController.topViewController?.supportedInterfaceOrientations
}
}
这篇关于supportedInterfaceOrientations无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!