网上大量的文章在适配iPad横竖屏时都使用了xib或storyboad, 但是xib和storyboard并不受团队开发欢迎,下面介绍采用纯代码的方式适配iPad的横屏和竖屏,方法非常简单:
1.) 设置要适配的对象的autoresizingMask, eg: targetView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
2.) 在当前的视图控制器中调用[self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0.0];
3.) 实现下面的方法:
/**
* 当屏幕即将旋转的时候调用
*
* @param toInterfaceOrientation 旋转完毕后的最终方向
* @param duration 旋转动画所花费的时间
*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { // 横屏
self.targetView.width = 150;
} else {
self.targetView.width = 50;
}
}
Demo下载地址:http://download.youkuaiyun.com/detail/luozhonglan/8378943