关于ios自动旋转的一点小事.

场景:应用只是一个portrait方向,很少能满足需求的.一般都要做旋转.旋转分自动和手工,自动比较容易实现,苹果在手工旋转没有提供比较好的api,自己做起来不是这出问题,就那出问题.所以我还是优先选择自动旋转,用起来苹果的自动旋转,其实体验没有想象中差.


设计:实现自动旋转只需要在window的rootViewController重写几个方法就行.如果rootViewController不是自定义类,比如UINavigationViewController,我觉得增加一个类别扩展,再重写方法也不是不错的主意.当然使用自定义子类去继承是没问题的.要兼容ios5,6,7,还是有点麻烦的,因为到ios6变了几个方法.


实现:



第一步:plist要声明添加支持的方向,这个key是UISupportedInterfaceOrientations

第二步:重写rootViewController方法.我的rootViewController是UINavigationViewController,并且使用了self.window.rootViewController=navController;写法.

在AppDelegate增加一个UINavigationViewController的类别Rotation如下:

@implementation UINavigationController (Rotation)
- (NSUInteger)supportedInterfaceOrientations {
    return (1 << UIInterfaceOrientationPortrait)|(1 << UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)shouldAutorotate {
    return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return toInterfaceOrientation==UIInterfaceOrientationPortrait||
        toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown;
}
@end

- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);和- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);是ios6的.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);是ios2-6的

如果考虑只兼容ios6+,shouldAutorotateToInterfaceOrientation方法可以不写ios6以下的用户还有5%左右吧,多少几行代码几关系.现在很多应该都兼容4.3+.

就这样,基本完了.细细节节再慢慢处理.


哦,说到细节:使用上面方法做的自动旋转,如果一个弹出窗口是使用keywindow实现的,你会发现不会自动旋转.解决这问题就是做弹出窗口不使用keywindow,比如上面可以showinView到UINavigationController的view完全没问题...不建议直接showInView到Sub View.因为在子view可能重写layoutsubviews,当使用了subview就会触发layoutsubviews,如果layoutsubviews做了不少事,可能会引起性能的问题.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值