横竖屏切换问题ios

来源:互联网 发布:如何用java制作表格 编辑:程序博客网 时间:2024/05/16 09:41
首先需要处理根视图 假如是navgationcontroller  写一个类继承与uinavgationcontroller 在这个子类中下以下代码;
-(BOOL)shouldAutorotate{    return YES;}-(NSUInteger)supportedInterfaceOrientations{    return [self.viewControllers.lastObject supportedInterfaceOrientations];}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];}
在每个viewcontroller中写以下的代码支持横竖屏:
- (BOOL)shouldAutorotate{    return NO;/返回YES 支持旋转}- (NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;/UIInterfaceOrientationMaskPortrait(支持竖屏) UIInterfaceOrientationMaskLandscapeLeft(支持home键向左)UIInterfaceOrientationMaskLandscapeRight (支持home键向右)UIInterfaceOrientationMaskPortraitUpsideDown (支持home键在上)UIInterfaceOrientationMaskLandscape(支持横屏)UIInterfaceOrientationMaskAll (支持各个方向)UIInterfaceOrientationMaskAllButUpsideDown(支持除了home键在上方的) }- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return UIInterfaceOrientationPortrait;}


如果根视图为UITabBarController 在tabbar的子类写如下代码

- (BOOL)shouldAutorotate{        //返回顶层视图的设置(顶层控制器需要覆盖shouldAutorotate方法)        NavRootViewController *nav = (NavRootViewController *)[self.viewControllers objectAtIndex:self.selectedIndex];        return nav.topViewController.shouldAutorotate;    //}////return NO;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    NavRootViewController *nav = [self.viewControllers objectAtIndex:self.selectedIndex];    return nav.topViewController.preferredInterfaceOrientationForPresentation;}//6.0之后系统调用该方法-(NSUInteger)supportedInterfaceOrientations{        //返回顶层视图支持的旋转方向        NavRootViewController *nav = (NavRootViewController *)[self.viewControllers objectAtIndex:self.selectedIndex];        return nav.topViewController.supportedInterfaceOrientations;    //    return 0;    }


以下是参考文档

iOS屏幕旋转控制(iOS6之后)
    iOS6之前,子控制器只要覆盖父类的shouldAutorotateToInterfaceOrientation:方法就能单独控制某个指定的控制器,而iOS6之后,该方法被禁止使用,让旋转控制变得复杂了,本文只针对iOS6以后的旋转控制做讲解;
    iOS6以后,系统会调用rootViewControllerA控制器的shouldAutorotate等方法进行方向控制,所以,如果用户想单独控制每个页面,则我们需要在Root控制器的shouldAutorotate方法中,将控制返回值交给某个指定的控制器来返回,window的rootViewController分以下几种情况:UIViewController、UINavigationController、UIMainTabController、MMDrawerController开源框架(挑选这个做讲解);

     一、跟视图UIViewController:
    该情况不需要多谢,由UIviewController自己控制就行;

    二、跟视图为UINavigationController(在UINavigationController中设置)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

   return[self.topViewControllershouldAutorotateToInterfaceOrientation:toInterfaceOrientation];

}

//6.0之后系统调用该方法

-(BOOL)shouldAutorotate

{

//系统会调用跟视图的旋转控制方法,所以我们将跟视图将控制条件交给顶层视图(顶层视图即我们需要控制的视图)

系统调用该方法

   returnself.topViewController.shouldAutorotate;

}

//6.0之后系统调用该方法,应该支持的方向

-(NSUInteger)supportedInterfaceOrientations

{

   returnself.topViewController.supportedInterfaceOrientations;

}


三、跟视图为UITabBarController(在UITabBarController中设置)

//6.0之后系统调用该方法

-(BOOL)shouldAutorotate

{

  //返回顶层视图的设置(顶层控制器需要覆盖shouldAutorotate方法

      UINavigationController*nav = (UINavigationController*)[self.viewControllersobjectAtIndex:mainTab.selectedIndex];

      returnnav.topViewController.shouldAutorotate;

   }

   return NO;

}

//6.0之后系统调用该方法

-(NSUInteger)supportedInterfaceOrientations

 

   //返回顶层视图支持的旋转方向

      UINavigationController*nav = (UINavigationController*)[self.viewControllersobjectAtIndex:mainTab.selectedIndex];

      returnnav.topViewController.supportedInterfaceOrientations;

   return 0; 

}

四、跟视图为MMDrawerController

//6.0一下系统调用该方法

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

   //返回顶层视图的设置

   if (self.openSide ==0) {

      //centerShow

      MainTabBarController*mainTab = (MainTabBarController*)self.centerViewController;

      UINavigationController*nav = (UINavigationController*)[mainTab.viewControllersobjectAtIndex:mainTab.selectedIndex];

      NSLog(@"nav.topViewController.supportedInterfaceOrientations : %d",nav.topViewController.shouldAutorotate);

       return[nav.topViewControllershouldAutorotateToInterfaceOrientation:toInterfaceOrientation];

   }

   return NO;

}


//6.0之后系统调用该方法

-(BOOL)shouldAutorotate

{

   //返回顶层视图的设置

   if (self.openSide ==0) {

      //centerShow

      MainTabBarController*mainTab = (MainTabBarController*)self.centerViewController;

      UINavigationController*nav = (UINavigationController*)[mainTab.viewControllersobjectAtIndex:mainTab.selectedIndex];

      NSLog(@"nav.topViewController.supportedInterfaceOrientations : %d",nav.topViewController.shouldAutorotate);

      returnnav.topViewController.shouldAutorotate;

   }

   return NO;

}


//6.0之后系统调用该方法

-(NSUInteger)supportedInterfaceOrientations

 

   //返回顶层视图支持的旋转方向

   if (self.openSide ==0) {

      //centerShow

      MainTabBarController*mainTab = (MainTabBarController*)self.centerViewController;

      UINavigationController*nav = (UINavigationController*)[mainTab.viewControllersobjectAtIndex:mainTab.selectedIndex];

      NSLog(@"nav.topViewController.supportedInterfaceOrientations; %d",nav.topViewController.supportedInterfaceOrientations);

      returnnav.topViewController.supportedInterfaceOrientations;

       

    }else if(self.openSide ==1){

      //leftShow

   }

   return 0;

}

五、presentViewController视图控制

   模态弹出来的视图,并不能在window的跟视图中控制,(因为它不在这个堆栈中?)如果模态视图为UINavigationcontrollor,则需要交给他自己的UINavigationcontrollor中控制。



0 0
原创粉丝点击