iOSiOS技术专题iOS开发

iOS屏幕旋转各类集锦(一)-单页旋转

2016-03-17  本文已影响958人  小布走慢点

如果你需要开启旋转
首先你需要开启旋转在info.plist点选(需要使用方向)

或者在AppDelegate中实现代理方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortraitUpsideDown;
}

系统提供的样式:无非是各种搭配一下和plist里的一样

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 <<     UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
}

看了这么久你会发现,道理我都懂但是咋使用呢?

****NAV PushViewController 单独设置某页****

注 : 使用UINavigationController 父类实现不然子VC无效(原因就是在有UINavigationController的情况下rootViewController是UINavigationController)

父类实现方法,就可以让某个单独的VC获得效果:

- (BOOL)shouldAutorotate  
{ 
 //也可以用topViewController判断VC是否需要旋转
return self.topViewController.shouldAutorotate;  
}  

- (NSUInteger)supportedInterfaceOrientations  
{   
    //也可以用topViewController判断VC支持的方向
    return self.topViewController.supportedInterfaceOrientations;  
}

****Tabbat PushViewController 单独设置某页****

- (BOOL)shouldAutorotate {
 return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
 return [self.selectedViewController supportedInterfaceOrientations];
}

tabbr+nav 需要同时设置才能生效

子类实现方法

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //当前支持的旋转类型
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
    // 是否支持旋转
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    // 默认进去类型
  return   UIInterfaceOrientationPortrait;
}

PresentViewController 单独设置某页

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 是否支持旋转
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    // 是否支持旋转
    return YES;
}

iOS屏幕旋转各类集锦(二)-单页部分旋转
***写的比较粗糙demo附上https://github.com/bloodspasm/ScreenRotation ***

上一篇 下一篇

猜你喜欢

热点阅读