源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解iOS中UIView的layoutSubviews子视图布局方法使用

  • 时间:2022-10-21 23:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解iOS中UIView的layoutSubviews子视图布局方法使用
[b]概念 [/b]在UIView里面有一个方法layoutSubviews:
[u]复制代码[/u] 代码如下:
- (void)layoutSubviews;    // override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
layoutSubviews方法的具体作用 layoutSubviews是对subviews重新布局。比如,我们想更新子视图的位置的时候,可以通过调用layoutSubviews方法,既可以实现对子视图重新布局。 layoutSubviews默认是不做任何事情的,用到的时候,需要在自雷进行重写。 [b]实例 [/b]由于ipad的横竖屏不同,所以好的应用,横竖屏的页面布局也不一样。那么就需要横竖屏的整体解决方案。先看一个横竖屏布局不一样的界面。 下面两张图是来自同一个界面的横竖版的截屏。可以看出,横竖版显示的内容相同,但是界面布局不同。要实现上述布局,主要是运用UIView中 layoutSubviews方法。当UIView设置为自动适配屏幕时,当用户旋转设备的时候,会调用layoutSubviews方法,我们只需重写 这个方法,然后判断用户屏幕的方向。在调整每个空间的位置即可。 [img]http://files.jb51.net/file_images/article/201604/201642791446629.jpg?201632791456[/img] [img]http://files.jb51.net/file_images/article/201604/201642791505056.jpg?201632791513[/img] 下面是实现上述界面的最简单的原型: 首先分析可以知道左面是图片,右面是一个图片加文字的视图。下面就实现一个左面视图右面是一个图加一段字的事例。 事例的截图如下 [img]http://files.jb51.net/file_images/article/201604/201642791524044.png?201632791535[/img] [img]http://files.jb51.net/file_images/article/201604/201642791543249.png?201632791551[/img] 其中右面的文字和绿色部分是用一个子视图封装的。 整个布局是我在主视图中添加了一个ContentView视图,在ContentView视图中添加了一个ArticleView视图。 其中ArticleView和ContentView的xib文件都打开了 [img]http://files.jb51.net/file_images/article/201604/201642791604398.png?201632791615[/img] 在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏。具体代码:
[u]复制代码[/u] 代码如下:
-(void)layoutSubviews{ [super layoutSubviews]; UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { //翻转为竖屏时 [self setVerticalFrame]; }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { //翻转为横屏时 [self setHorizontalFrame]; } } -(void)setVerticalFrame { NSLog(@"竖屏"); [titleLable setFrame:CGRectMake(283, 0, 239, 83)]; [leftView setFrame:CGRectMake(38, 102, 384, 272)]; [rightView setFrame:CGRectMake(450, 102, 282, 198)]; } -(void)setHorizontalFrame { NSLog(@"横屏"); [titleLable setFrame:CGRectMake(183, 0, 239, 83)]; [leftView setFrame:CGRectMake(168, 122, 384, 272)]; [rightView setFrame:CGRectMake(650, 122, 282, 198)]; }
在具体的横竖屏方法中,从新设置各个组件的坐标即可。 接下来在ContentView中添加ArticleView视图。
[u]复制代码[/u] 代码如下:
-(id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil]; rightView=[arrayContentView objectAtIndex:0]; [self addSubview:rightView]; } return self; }
由于我用的是xib,所以初始化方法为initWithCoder,在这个中添加新的视图。 同样在ArticleView中设置横竖屏相应空间的坐标即可。
[u]复制代码[/u] 代码如下:
-(void)layoutSubviews{ [super layoutSubviews]; UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; CGRect rect=self.frame; rect.size.width=282; rect.size.height=198; [self setFrame:rect]; if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { //翻转为竖屏时 [self setVerticalFrame]; }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { //翻转为横屏时 [self setHorizontalFrame]; } } -(void)setVerticalFrame { NSLog(@"竖屏"); [contentView setFrame:CGRectMake(12, 6, 250, 125)]; [textLable setFrame:CGRectMake(50, 139, 182, 39)]; } -(void)setHorizontalFrame { NSLog(@"横屏"); [contentView setFrame:CGRectMake(12, 6, 106, 158)]; [textLable setFrame:CGRectMake(135, 11, 147, 39)]; }
[b]总结 [/b]layoutSubviews以下情况会被调用: 苹果官方文档已经强调,不能直接调用layoutSubviews对子视图进行重新布局。那么,layoutSubviews什么情况下会被调用呢?通过百度搜索,发现以下几种情况layoutSubviews会被调用。 [list] [*]直接调用setLayoutSubviews。(这个在上面苹果官方文档里有说明)[/*] [*]addSubview的时候。[/*] [*]当view的frame发生改变的时候。[/*] [*]滑动UIScrollView的时候。[/*] [*]旋转Screen会触发父UIView上的layoutSubviews事件。[/*] [*]改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。[/*] [/list] 我简单测试了一下,上面基本都会被调用。 注意: 当view的fram的值为0的时候,`addSubview`也不会调用`layoutSubviews`的。 layoutSubviews方法在对自雷视图进行布局的时候非常方便。可以自己动手,深入理解layoutSubviews的调用机制。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部