问题描述
在我的应用程序中,我既有tabbar又有navigationBar。 rootview controller tabbar和tabbar有4个导航控制器。
In my app i've both tabbar and navigationBar. rootview controller tabbar and tabbar has 4 navigation controller.
我想让一些viewcontrollers只是肖像。这可能是一个常见的问题,但我已经尝试了但我无法解决这个问题。
I want to make some viewcontrollers to be portrait only. It may be a common question but I've tried enough but I could not solve this.
如何为某些视图控制器设置纵向?
How to make portrait orientation for some view Controller?
推荐答案
我已经解决了这个问题并回答,如果有人遇到同样的问题他们可以得到帮助。
I've solved this problem and answering so that if someone face same problem they can get a help.
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
以上如果它们位于navigationcontroller的任何tabbarcontroller内,则不会调用viewcontroller。如果在tabbarcontroller或导航控制器中声明了这些方法,那么它们将被调用。在我的例子中,viewcontrollers在导航控制器内部,导航控制器在tabbarcontroller内。
The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called. In my case the viewcontrollers was inside navigationcontroller and the navigation controllers are inside a tabbarcontroller.
为了解决这个问题,我创建了一个类FixedOrientationTab,它是UITabBarController的子类,并且导航类OrientationEnabledNavigation,它是UINavigationController的子类。然后我在FixedOrientationTab和OrientationEnabledNavigation中实现了 shouldAutorotate,supportedInterfaceOrientations,preferredInterfaceOrientationForPresentation
方法。
For solving this I make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then I implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
methods inside FixedOrientationTab and OrientationEnabledNavigation.
OrientationEnabledNavigation.h
#import <UIKit/UIKit.h>
@interface OrientationEnabledNavigation : UINavigationController
@end
OrientationEnabledNavigation.m
#import "OrientationEnabledNavigation.h"
@interface OrientationEnabledNavigation ()
@end
@implementation OrientationEnabledNavigation
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
FixedOrientationTab.h
#import <UIKit/UIKit.h>
@interface FixedOrientationTab : UITabBarController
@end
FixedOrientationTab.m
#import "FixedOrientationTab.h"
@interface FixedOrientationTab ()
@end
@implementation FixedOrientationTab
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后如果你想使用然后,项目中的导航控制器使用OrientationEnabledNavigation和tabbar FixedOrientationTab。之后如果你在viewcontroller中实现 shouldAutorotate,supportedInterfaceOrientations,preferredInterfaceOrientationForPresentation
这些方法,那么它们就会被调用。
Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
these method inside your viewcontroller then they will get called.
Hope这有助于...... :)
Hope this helps.. :)
这篇关于某些视图控制器的固定方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!