ViewController的旋转一个举例
来源:互联网 发布:python xml dom 编辑:程序博客网 时间:2024/04/29 18:35
假设手机的四个方向分别是:1(Portrait),2(PortraitUpSideDown),3(LandscapeRight),4(LandscapeLeft)
假设一个工程有如下的界面旋转需求(支持iOS5.0及其以上版本需求)
只支持方向1,有如下A,B,C,D界面
支持方向1,方向3,方向4,有如下的界面:E
根据网上的资料和最新的相关文档,需要如下的实现:
在AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
AViewController *vc = [[AViewControlleralloc]init];
UINavigationController *nc = [[UINavigationControlleralloc]initWithRootViewController:vc];
self.window.rootViewController = nc;
[self.windowmakeKeyAndVisible];
return YES;
}
因为给window的rootViewController是UINavigationController
要在iOS6.0中支持旋转,所以要实现如下的类别:
@implementation UINavigationController (Rotation_IOS6)
// iOS6.0
- (BOOL)shouldAutorotate
{
returnself.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
returnself.topViewController.supportedInterfaceOrientations;
}
@end
因为ABCDE的ViewController都是同一种,假设ABCDE 都继承于BaseViewController,需要在BaseViewController,添加如下代码:
#pragma mark - befor iOS6.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
#pragma mark - iOS6.0 and later
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
returnUIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
returnUIInterfaceOrientationPortrait;
}
ABCD界面已经满足需求了
因为E的旋转需求不一样,所以E中要重写上述的三个函数:
// before iOS6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation ==UIInterfaceOrientationLandscapeRight || interfaceOrientation ==UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationPortrait;
}
// iOS6 or later
- (NSUInteger)supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
这样,无论ABCDE界面是push还是present都可以满足需求了
- ViewController的旋转一个举例
- 旋转ViewController
- iOS 的Viewcontroller只支持一个屏幕方向,弹出不同方向,旋转
- UITabbarcontroller里的viewcontroller旋转控制
- present 一个透明的viewController
- 一个域名解析的举例
- 显示一个透明背景的ViewController在另一个ViewController上
- mvc,ViewController,旋转
- 关于多个viewcontroller和nib文件旋转的问题
- 关于iOS控制单个viewcontroller旋转需要注意的地方
- 推出一个透明的Viewcontroller控制器
- present一个背景透明的viewController
- 一个和viewcontroller生命周期相关的bug
- 一个简单的触发器举例
- prensent一个半透明的viewcontroller 或者push 出一个viewcontroller 做present 动画
- 多个ViewController 只隐藏一个ViewController的UINavigationBar 和UITabBar
- 从一个ViewController 跳转到另一个ViewController 时遇到的问题
- 一个ViewController不能加载
- 网络图片的获取和显示的方法
- eCos中的main函数
- 2011成都网选最水的几道题
- ORA-01078, LRM-00123错误处理
- ACM STEP 1.2.5 GPA
- ViewController的旋转一个举例
- UVa 127 - "Accordian" Patience
- 一道百度面试题
- 在linux下使用mysql的基础操作(二)
- ACM STEP 1.2.7 Lowest Bit
- 第四章总结(上)
- 【PAT Advanced Level】1004. Counting Leaves (30)
- Linux就是这个范儿之第一次亲密接触(3)
- WEB开发总结(持续更新。。。)