iOS: autosizing

若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,10100100);


建议uiview frame的设置的代码写在willAnimateRotationToInterfaceOrientation里 ,而shouldAutorotateToInterfaceOrientation方法则是用于设置支持哪些方向的rotation.

注意:willAnimateRotationToInterfaceOrientation在启动app时不会调用,只有在rotation之后才会调用,而shouldAutorotateToInterfaceOrientation在启动时也会调用,而且会调用超过一次!

Example:

[cpp]  view plain copy
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     NSLog(@"shouldAutorotateToInterfaceOrientation");  
  4.     return YES;  
  5. }  
  6.   
  7. - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{  
  8.     NSLog(@"willAnimateRotationToInterfaceOrientation");  
  9.       
  10.     if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||  
  11.         toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)  
  12.     {  
  13.         view1.frame=CGRectMake(10, 10, 100, 100);  
  14.     }  
  15.     else  
  16.     {  
  17.         view1.frame=CGRectMake(50, 50, 50, 50);  
  18.     }      
  19. }  


ref links:

http://www.cnblogs.com/xingchen/archive/2012/02/29/2374798.html

http://www.techotopia.com/index.php/IOS_4_iPhone_Rotation,_View_Resizing_and_Layout_Handling

http://blog.csdn.net/yuquan0821/article/details/7596545

http://www.cocoachina.com/bbs/read.php?tid=98457

http://hi.baidu.com/rslhg/blog/item/c5338dbf03fa701318d81f64.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值