您的当前位置:首页正文

iphone横竖屏的适配

来源:要发发知识网

我的项目中个别的界面需要旋转屏幕,有的是不需要旋转的

解决如下:就是把系统类子类化,再使用子类

1.设置app级别的支持屏幕的方向

targets -> General -> Deployment Info

2.所有界面都有一个统一的BaseViewController,在这里实现ViewController级别的设置屏幕方向

3.在tabbarController中设置屏幕方向

4.在NavigationController中设置屏幕方向

5.在要旋转的页面设置屏幕旋转如下:

要旋转的view是通过这样展示

[self presentViewController:perview animated:NO completion:nil];

我这里应该是最笨的方法,屏幕旋转重新添加subviews,有待优化

在开发中遇到的需要注意的问题

1、UIAlertViewcontroller

iOS 8 UIActionSheet ignores view controller supportedInterfaceOrientations and shouldAutorotate

我做了一个·类扩展

```

-(void)viewWillAppear:(BOOL)animated

{

if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {

[[UIDevicecurrentDevice]performSelector:@selector(setOrientation:)withObject:UIPrintInfoOrientationPortrait];

}

}

```

还看了一些stackoverflow中的问答

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

UIViewController *presentedViewController = window.rootViewController.presentedViewController;

if (presentedViewController) {

if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) {

return UIInterfaceOrientationMaskPortrait;

}

}

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;(这是view controller级别的屏幕方向)

}

2、

deprecated表示已不被建议使用,可能随时取消它;建议采用新的来替代

3、相关链接:

#if 0

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations.

return YES;

}

#endif

#if 1

- (BOOL)shouldAutorotate

{

return YES;

}

- (NSUInteger)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskAllButUpsideDown;

}

#endif

BaseTabBarController  BaseNavigationController

我都是先把系统类子类化 然后使用子类

我所有的页面都有一个统一的 BaseViewController 、

这里面实现了 #pragma mark 旋转

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

return UIInterfaceOrientationPortrait;

}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskPortrait;

}

-(BOOL)shouldAutorotate

{

return NO;

}

然后在要旋转的页面 加入下面代码

- (BOOL)shouldAutorotate

{

return NO;

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

return UIInterfaceOrientationLandscapeRight;

}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskLandscapeRight;

}