在iOS应用的开发过程当中,常常会使用,setFrame的方式对UIView进行布局,app
常常会使用计算的方式,如self.view.bounds.size.height - 20-44- Heignt等来计算Y的相对位置ide
咱们知道上边的数字 20是status bar的高度,44是navigationBar的高度.布局
这样的写法没有什么错误,可是不利于代码的复用,好比一个ViewController在建立的时候,有可能有navigationController,也可能没有navigationController,在这种状况下,这个VIewController里边的子UIView的相对位置就可能出现误差.url
因此,本文主要介绍autoresizingMask属性,对UIVIew进行相对的布局。spa
假设以下的需求:.net
程序启动后,构建一个自定义的TabBar,始终显示在应用的底部,不管屏幕发生旋转,或者收到来电的状况下,都显示在应用的底部。(看起来,跟如今的不少微博客户端类似,它们多半都没有使用系统的tabbarcontroller方式,而是本身绘制的tabbar).orm
能够用以下的代码来实现blog
- (void)viewDidLoad ip
{ 开发
[super viewDidLoad];
self.tabbar = [[[CustumTabBar alloc] init] autorelease];
[self.tabbar setFrame:CGRectMake(0, self.view.bounds.size.height -44, self.view.bounds.size.width, 44)];
self.tabbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
[self.tabbar setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:self.tabbar];
}
同理,若是想让一个UIVIew始终都在屏幕中心,
能够设置它的Y为 ceilf((self.view.bounds.size.height - Height)/2),
同时设置autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin 便可。