UIView的AutoSizing
2013-10-02 21:25
351 查看
若app支持旋转(rotation),那么几乎一定会涉及UIView的Autosizing问题。
Autosize有2种方法:
一是在NB的size inspectator property panel里设置;
一是用代码设置uiview的autoresizingMask属性;
注意: 对于margin,在NB里的设置和用代码设置逻辑上是相反的。
例子:
①要uiview基于屏幕左上角 (top & left) 的位置不变,
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin;
而对应的NB设置则是:
②要uiview对于上下左右的margin都是flexible的 (即rotate时上下左右都要调整margin)
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
而对应的NB设置则是:
③要uiview对于上下左右的margin都是fix的 (如果你希望uiview旋转时不需要autosizing,即手动写代码来设置位置和大小,那么应该使用这个)
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingNone;
而对应的NB设置则是:
配合上面的设置,你可能还需要手动写代码来设置uiview的位置和大小,example
view1.frame=CGRectMake(10,10, 100, 100);
建议uiview frame的设置的代码写在willAnimateRotationToInterfaceOrientation里 ,而shouldAutorotateToInterfaceOrientation方法则是用于设置支持哪些方向的rotation.
注意:willAnimateRotationToInterfaceOrientation在启动app时不会调用,只有在rotation之后才会调用,而shouldAutorotateToInterfaceOrientation在启动时也会调用,而且会调用超过一次!
Example:
[cpp] view
plaincopy
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(@"shouldAutorotateToInterfaceOrientation");
return YES;
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"willAnimateRotationToInterfaceOrientation");
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
view1.frame=CGRectMake(10, 10, 100, 100);
}
else
{
view1.frame=CGRectMake(50, 50, 50, 50);
}
}
Autosize有2种方法:
一是在NB的size inspectator property panel里设置;
一是用代码设置uiview的autoresizingMask属性;
注意: 对于margin,在NB里的设置和用代码设置逻辑上是相反的。
例子:
①要uiview基于屏幕左上角 (top & left) 的位置不变,
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin;
而对应的NB设置则是:
②要uiview对于上下左右的margin都是flexible的 (即rotate时上下左右都要调整margin)
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
而对应的NB设置则是:
③要uiview对于上下左右的margin都是fix的 (如果你希望uiview旋转时不需要autosizing,即手动写代码来设置位置和大小,那么应该使用这个)
如果要代码则应该是
subView.autoresizingMask = UIViewAutoresizingNone;
而对应的NB设置则是:
配合上面的设置,你可能还需要手动写代码来设置uiview的位置和大小,example
view1.frame=CGRectMake(10,10, 100, 100);
建议uiview frame的设置的代码写在willAnimateRotationToInterfaceOrientation里 ,而shouldAutorotateToInterfaceOrientation方法则是用于设置支持哪些方向的rotation.
注意:willAnimateRotationToInterfaceOrientation在启动app时不会调用,只有在rotation之后才会调用,而shouldAutorotateToInterfaceOrientation在启动时也会调用,而且会调用超过一次!
Example:
[cpp] view
plaincopy
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(@"shouldAutorotateToInterfaceOrientation");
return YES;
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"willAnimateRotationToInterfaceOrientation");
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
view1.frame=CGRectMake(10, 10, 100, 100);
}
else
{
view1.frame=CGRectMake(50, 50, 50, 50);
}
}
相关文章推荐
- easyui简单使用
- POJ 2299 Ultra-QuickSort(归并排序求逆序数)
- 移动应用界面原型构建工具,交互设计师的利器
- Ubuntu 编译内核-"mkimage" command not found - U-Boot images will not be built
- Java多线程之BlockingQueue(一)
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
- [leetcode]N-Queens II
- 【C】——APUE小程序之递归遍历目录
- [leetcode]N-Queens
- 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解
- RMQ(Range Minimum/Maximum Query)-ST算法
- Android requires compiler compliance 错误解决方法
- uva10534 - Wavio Sequence O(nlgn)的最长上升子序列
- request 获取各项参数以及意义
- 2014年Baidu的Interview Question(02)【转载】
- 2014年Baidu的Interview Question(01)【转载】
- [LeetCode] Unique Binary Search Trees II
- Android开发优化之——对界面UI的优化
- 各种字体(在uilabel和uitextview中要常用)
- QuickFIX/J常见问题汇总