supportedInterfaceOrientations not working
ios, orientation
Solution
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;
}
Now your NavigationController should be a subclass of the UINavigationContorller parent
Swift 3:
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
}
Update Taken from Matt's answer if you don't want to subclass:
first: make your viewController a delegate of the navigationController in `viewDidLoad`
self.navigationController?.delegate = self
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
}
}
Problem
I want some of my ViewControllers landscape and some portrait so this is what I did: I enabled landscape mode: 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?