旋屏操作

在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如

  1. -(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的单独控制是无法锁定屏幕的。


  1. -(NSUInteger)supportedInterfaceOrientations  
  2. {  
  3.     return UIInterfaceOrientationMaskPortrait;  
  4. }  


在参考了一些博客,之后,自己写了一个小工程,感觉可以实现指定方向的旋转。

首先建立三个类,一个是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;

}



然后再RootViewController中这样写

- (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;

}


最后在SecondViewController中

- (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中设置。

  1. - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  
  2. {  
  3.      return UIInterfaceOrientationMaskPortrait;  
  4. }  

好了,你设置该应用为允许左右上下左右旋屏时,刚进去的第一个界面是不旋屏的,在你点击button时,会发现屏幕朝右旋转,而且只朝这一个方向。
本文部分参考自念茜的博客。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值