ios中layouios insertsubvieww何时被调用

安全检查中...
请打开浏览器的javascript,然后刷新浏览器
< 浏览器安全检查中...
还剩 5 秒&layoutSubviews在什么情况下调用 - 简书
layoutSubviews在什么情况下调用
1.在以下情况都会调用
1.直接调用[self setNeedsLayout];(这个在上面苹果官方文档里有说明)
2.addSubview的时候。
3.当view的size发生改变的时候。
4.滑动UIScrollView的时候。
5.旋转Screen会触发父UIView上的layoutSubviews事件。注意:当view的size的值为0的时候,addSubview也不会调用layoutSubviews。当要给这个view添加子控件的时候不管他的size有没有值都会调用
2.先来看一下UIView的layoutSubviews在什么情况下会调用
2.1初始化,当size不为0的时候调用一次,当size为0的时候不会调用(在任何方法里面都不会调用)SubView*view= [[SubView alloc]initWithFrame:CGRectMake(0,0,100,100)];
view.backgroundColor=[UIColor redColor];
[self.view addSubview:view];
self.sbView=
2.2当size改变的时候调用一次(size每次都不同,如果size相同就不会调用)
2.3当添加子控件的时候,不管子控件有没有尺寸都会调用例如:// 添加子控件的时候都会调用
CustomBtn *button = [[CustomBtn alloc] init];
[self.sbView addSubview:button];
3.看UIButton什么时候调用
3.1这种情况下button的layoutSubviews调用一次,因为当控制器的view显示的时候会调用控制器view的layoutSubviews(UIView除外,比较特殊)- (void)viewDidLoad{
[super viewDidLoad];
CustomBtn*button = [[CustomBtn alloc]init];
[self.view addSubview:button];
如果放在 viewDidAppear button的layoutSubviews就不会调用- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// 因为view这时候已经显示了,父控件就不会布局了,所以代码写在这里最好,受干扰的因素最小
CustomBtn*button = [[CustomBtn alloc]init];
[self.view addSubview:button];
3.2设置title会调用一次layoutSubviewsCustomBtn*button = [[CustomBtn alloc] init];
[button setTitle:@"xxoo" forState:UIControlStateNormal];
[self.view addSubview:button];
3.2设置image的时候会调用一次layoutSubviews,注意:如果图片的名字在工程里面没有,就不会调用,别想骗过苹果CustomBtn *button = [[CustomBtn alloc] init];
[button setImage:[UIImage imageNamed:@"xoxo"] forState:UIControlStateNormal];
[self.view addSubview:button];
4.UIScrollView
4.1这种情况也会调用(比较特殊)// scrollView比较特殊
CustomScrollView *scrollView= [[CustomScrollView alloc]init];
[self.view addSubview:scrollView];
4.2这种情况会调用一次layoutSubviews(代码放在viewDidAppear,防止干扰)// 因为scrollView有size,所以会调用
CustomScrollView *scrollView= [[CustomScrollView
alloc]initWithFrame:CGRectMake(100,100,300,200)];
[self.view addSubview:scrollView];
scrollView.backgroundColor= [UIColor yellowColor];
scrollView.contentSize= CGSizeMake(0,500);
4.3当UIScrollView滑动的时候会掉多次layoutSubviews
持续更新实用的干货
微博: coderYJ
微信公众号 coderYJ1133人阅读
iOS(162)
从百度上搜索了一下layoutSubviews的用处,以下是搜索的结果,当然,笔者是会一一验证的.
1、&init初始化不会触发layoutSubviews
2、&addSubview会触发layoutSubviews
3、&设置view的Frame会触发layoutSubviews,当然前提是frame的&#20540;设置前后发生了变化
4、&滚动一个UIScrollView会触发layoutSubviews
5、&旋转Screen会触发父UIView上的layoutSubviews事件
6、&改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
在开始验证之前,先看看layoutSubviews到底是啥来着:)
Lays out subviews.
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
在iOS5.1或之前的版本中,这个方法什么也没干.这个方法的默认实现是&用参数来设定subviews的尺寸和位置的&.&
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation
to set the frame rectangles of your subviews directly.&
如果你需要更加精确的布局,可以在子类里面重写这个方法.仅仅在以下情况下:自动布局达不到你想要效果时你才有必要重写这个方法.你可以直接设置subviews的尺寸.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.&
你不能直接调用这个方法.如果你需要强制layout刷新,调用setNeedsLayout来代替.如果你想要立即刷新你的view,调用layoutIfNeeded
大概总结以下就是:
你不要直接调用方法layoutSubviews,如果想要刷新,请调用&setNeedsLayout&或者&layoutIfNeeded
好了,开始验证:)
现在提供继承至UIView的类如下:
#import &UIKit/UIKit.h&
@interface TestView : UIView
#import &TestView.h&
@implementation TestView
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
NSLog(@&initWithFrame:%@& ,NSStringFromCGRect(frame));
- (void)layoutSubviews
NSLog(@&layoutSubviews %@&, self);
[super layoutSubviews];
#import &RootViewController.h&
#import &TestView.h&
@interface RootViewController ()
@property (nonatomic, strong) NSTimer
@property (nonatomic, strong) TestView
@property (nonatomic, strong) TestView
@implementation RootViewController
- (void)viewDidLoad
[super viewDidLoad];
[self test_5];
- (void)test_1
走了initWithFrame:方法,但是又有frame&#20540;为{{0, 0}, {0, 0}},并不需要绘制任何的东西,
所以即使添加了test,也没必要绘制它,同时也验证了addSubview会触发layoutSubviews是错
误的,只有当被添加的view有着尺寸的时候才会触发layoutSubviews
TestView *test = [TestView new];
[self.view addSubview:test];
- (void)test_2
TestView *test = [TestView new];
test.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:test];
- (void)test_3
layoutSubviews这个方法自身无法调用,是被父类添加的时候才执行的方法
TestView *test = [TestView new];
test.frame = CGRectMake(0, 0, 50, 50);
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[test addSubview:showView];
- (void)test_4
CGRect rect = self.view.
CGFloat height = rect.size.
CGFloat width
= rect.size.
UIScrollView *rootScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
NSArray *data
= @[@&&, @&&, @&&, @&&];
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
TestView *tmp
= [[TestView alloc] initWithFrame:CGRectMake(width*idx, 0,
width, height)];
[rootScroll addSubview:tmp];
rootScroll.contentSize
= CGSizeMake(width * data.count, height);
[self.view addSubview:rootScroll];
- (void)test_5
_timer = [NSTimer scheduledTimerWithTimeInterval:1.f
target:self
selector:@selector(timerEvent:)
userInfo:nil
repeats:YES];
_largeView = [[TestView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_largeView];
_smallView = [[TestView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[_largeView addSubview:_smallView];
- (void)timerEvent:(id)sender
_smallView.frame = CGRectMake(arc4random()%100 &#43; 20,
arc4random()%100 &#43; 20,
arc4random()%100 &#43; 20,
arc4random()%100 &#43; 20);
NSLog(@&_smallView %@&, _smallView);
NSLog(@&_smallView %@&, _largeView);
测试后的结论是这样子的:
1. 一个view是不能够自己调用layoutSubviews,如果要调用,需要调用&setNeedsLayout或者&layoutIfNeeded
2. 如果view的frame&#20540;为0,即使被添加了耶不会调用layoutSubviews
3. 如果一个view的frame&#20540;改变了,那么它的父类的layoutSubviews也会被执行
layoutSubviews对subviews重新布局
layoutSubviews方法调用先于drawRect
setNeedsLayout在receiver标上一个需要被重新布局的标记,在系统runloop的下一个周期自动调用layoutSubviews
layoutIfNeeded方法如其名,UIKit会判断该receiver是否需要layout.根据Apple官方文档,layoutIfNeeded方法应该是这样的
&layoutIfNeeded遍历的不是superview链,应该是subviews链
drawRect是对receiver的重绘,能获得context
setNeedDisplay在receiver标上一个需要被重新绘图的标记,在下一个draw周期自动重绘,iphone device的刷新频率是60hz,也就是1/60秒后重绘
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:846878次
积分:13463
积分:13463
排名:第967名
原创:436篇
转载:480篇
评论:85条
(1)(1)(6)(2)(4)(3)(12)(4)(4)(3)(26)(12)(6)(14)(5)(1)(14)(18)(26)(37)(24)(29)(19)(9)(14)(17)(54)(20)(53)(127)(93)(49)(31)(44)(28)(56)(21)(5)(9)(12)(3)(1)}

我要回帖

更多关于 ios view layout 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信