Auto Layout 是IOS 6.0才开始引用的技术,如果要兼容IOS6.0以下程序,则不能启用Auto Layout,它能解决屏幕旋转界面呈现混乱的总是,
如果有人使用过网页的响应式布局的话,那么它感觉就与网页响应式布局相似,它不仅能设置控件的位置与大小,而且还可以放大,缩小处理。
如果是简单的设置则可以使用autolayout,如果是复杂的话,我觉得还是要通过代码实现,根据用户旋转屏幕。以上代码中没有写任何代码,全都是在
storyboard中完成。
以下是屏幕旋转后触发的事件。
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
UIInterfaceOrientation orientation=[[UIApplication sharedApplication] statusBarOrientation];
//do whatever;
CGRect rect;
switch (orientation) {
case UIInterfaceOrientationPortrait:
NSLog(@"UIInterfaceOrientationPortrait");
rect=self.topLeft.frame;
rect.size.width=130;
rect.size.height=200;
self.topLeft.frame=rect;
rect=self.topRight.frame;
rect.origin.x=170;
rect.size.width=130;
rect.size.height=200;
self.topRight.frame=rect;
rect=self.bottom.frame;
rect.origin.y=240;
rect.size.width=280;
rect.size.height=200;
self.bottom.frame=rect;
break;
// doesn't supoort.
// case UIInterfaceOrientationPortraitUpsideDown:
// NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
// break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"UIInterfaceOrientationLandscapeLeft");
// break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"UIInterfaceOrientationLandscapeRight");
//top left view;
rect=self.topLeft.frame;
rect.size.width=210;
rect.size.height=120;
self.topLeft.frame=rect;
//top rigth view
rect=self.topRight.frame;
rect.origin.x=250;
rect.size.width=210;
rect.size.height=120;
self.topRight.frame=rect;
//bottom view
rect=self.bottom.frame;
rect.origin.y=160;
rect.size.width=440;
rect.size.height=120;
self.bottom.frame=rect;
break;
default:
break;
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
//
NSLog(@"rotation completed.");
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}