autoresizingMask
来源:互联网 发布:网吧吃鸡优化 编辑:程序博客网 时间:2024/06/16 07:48
本文主要介绍autoresizingMask属性,对UIVIew进行相对的布局。
假设如下的需求:
程序启动后,构建一个自定义的TabBar,始终显示在应用的底部,无论屏幕发生旋转,或者收到来电的情况下,都显示在应用的底部。(看起来,跟现在的很多微博客户端相似,它们多半都没有使用系统的tabbarcontroller方式,而是自己绘制的tabbar).
可以用如下的代码来实现
- (void)viewDidLoad { [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]; }
代码中,autoresizingMask设置为底部和宽度对齐,加上shouldAutorotateToInterfaceOrientation方法返回YES后,程序无论怎样转向,TabBar都会在应用的底部显示了。
同理,如果想让一个UIVIew始终都在屏幕中心,
可以设置它的Y为 ceilf((self.view.bounds.size.height - Height)/2),
同时设置autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin 即可。
做一些简单的说明
setAutoresizingMask控件的自适应
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
UIViewAutoresizingNone
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
[label setAutoresizingMask: UIViewAutoresizingNone]; 控件相对于俯视图坐标值不变
CGRectMake(50, 100, 200, 40)
UIViewAutoresizingFlexibleWidth:控件的宽度随着父视图的宽度按比例改变 例如
label宽度为 100 屏幕的宽度为320 当屏幕宽度为480时 label宽度 变为 100*480/320
同理
UIViewAutoresizingFlexibleHeight相同
UIViewAutoresizingFlexibleLeftMargin 到屏幕左边的距离随着父视图的宽度按比例改变
例如
CGRectMake(50, 100, 200, 40)];
50 屏幕的宽度为320 当屏幕宽度为480时 label宽度 变为 50*480/320
控件变为 CGRectMake(75, 100, 200, 40)];
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
简单归结如下:
屏幕旋转如果想控制某个控件相对view的位置不发生变化(比如:右上角,右下角)应该清楚view在屏幕中心的水平、垂直位置(将屏幕划分为4个区域,屏幕左上角,屏幕左下角,屏幕右上角,屏幕右下角),只有view的坐标不在屏幕的左上角的view才需要调整autoresizingMask的属性。
例如:
基于屏幕 左下角 的view想要相对 Bottom & Left 的位置不变则 autoresizingMask= UIViewAutoresizingFlexibleTopMargin (IB方向相反,选择下方的Autoresizing)
基于屏幕 右上角 的view想要相对 Top & Right 的位置不变则 autoresizingMask= UIViewAutoresizingFlexibleLeftpMargin (IB方向相反,选择左方的Autoresizing)
基于屏幕 右下角 的view想要相对 Bottom & Right 的位置不变则 autoresizingMask= UIViewAutoresizingFlexibleTopMargin| UIViewAutoresizingFlexibleLeftMargin (IB方向相反,选择下方和右方的Autoresizing)
因为 屏幕右下角的 view 在屏幕水平位置和垂直位置的共有区域 所以要设置 TopMargin & LeftMargin
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask
- autoresizingMask 属性
- autoresizingMask ,, UIViewAutoresizingFlexibleWidth
- UIView autoresizingMask
- AutoresizingMask属性
- autoresizingMask 属性
- ios AutoresizingMask
- autoresizingMask 使用
- UIView autoresizingMask
- autoresizingMask解释
- autoresizingMask属性
- 第一节 线性回归和梯度下降
- OpenGL学习笔记-2015.4.1——纹理加载与控制
- CSS样式声明 元素属性 样式属性
- hdu 1083 Courses (最大边独立集)
- MFC(2)MFC中使用信号量(semaphore)做线程同步——用户启动子线程打开串口
- autoresizingMask
- 【SQL0001】Query from the result of another query
- POJ1042 贪心钓鱼
- PAT(BASIC)1033. 旧键盘打字(20)
- javascript 跨域方法总结
- Add Binary--LeetCode
- hdu4642博弈(矩阵)
- @EView@EViewGroup增强的自定义视图
- 在数据库中用 UUID 来做ID