在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如
-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但是在iOS6中,这个方法被废弃了,无法使用。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)
实践后会发现,通过supportedInterfaceOrientations的单独控制是无法锁定屏幕的。
- -(NSUInteger)supportedInterfaceOrientations
- {
- return UIInterfaceOrientationMaskPortrait;
- }
在参考了一些博客,之后,自己写了一个小工程,感觉可以实现指定方向的旋转。
首先建立三个类,一个是NavgationViewController,继承自UINavigationController,另外两个都是建立继承自
UIViewController的RootViewController和SecondViewController
好了,我们现在在AppDelegate这样写
RootViewController *rvc=[[RootViewController alloc]init];
NavgationViewController *nvc=[[NavgationViewController alloc]initWithRootViewController:rvc];
self.window.rootViewController=nvc;
在NavgationViewController这么写
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
// return [self.viewControllers.lastObject supportedInterfaceOrientations];
return self.topViewController.supportedInterfaceOrientations;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
return self.topViewController.supportedInterfaceOrientations;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
[but setTitle:@"点击" forState:UIControlStateNormal];
[but setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
but.frame=CGRectMake(100,100 , 44, 44);
[but addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
-(void)buttonClick:(UIButton *)but{
SecondViewController *vc=[[SecondViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 320, 44)];
label.text=@"hello";
label.backgroundColor=[UIColor blackColor];
[self.view addSubview:label];
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
[but setTitle:@"点击" forState:UIControlStateNormal];
[but setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
but.frame=CGRectMake(220,100 , 44, 44);
[but addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
-(void)buttonClick:(UIButton *)but{
// RootViewController *vc=[[RootViewController alloc]init];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;//这里控制向哪旋转
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;//这里控制向哪旋转
}
/*
该方法可让所有的Viewcontroller都不支持旋屏。当然也可以在General中的info中设置。
- - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
- {
- return UIInterfaceOrientationMaskPortrait;
- }
好了,你设置该应用为允许左右上下左右旋屏时,刚进去的第一个界面是不旋屏的,在你点击button时,会发现屏幕朝右旋转,而且只朝这一个方向。