解析LayoutSubviews

简介:

解析LayoutSubviews

从百度上搜索了一下layoutSubviews的用处,以下是搜索的结果,当然,笔者是会一一验证的.

1、init初始化不会触发layoutSubviews

2、addSubview会触发layoutSubviews

3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化

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的类如下:

//
//  TestView.h
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TestView : UIView

@end


//
//  TestView.m
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSLog(@"initWithFrame:%@" ,NSStringFromCGRect(frame));
    }
    return self;
}

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews %@", self);
    [super layoutSubviews];
}

@end

测试代码:
//
//  RootViewController.m
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "TestView.h"

@interface RootViewController ()

@property (nonatomic, strong) NSTimer   *timer;
@property (nonatomic, strong) TestView  *largeView;
@property (nonatomic, strong) TestView  *smallView;

@end

@implementation RootViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    // 1、init初始化不会触发layoutSubviews [正确的]
    // 2、addSubview会触发layoutSubviews [不完全正确,当frame为0时是不会触发的]
    // 3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化 [正确]
    
//    [self test_1];
//    [self test_2];
//    [self test_3];
    
    // 4、滚动一个UIScrollView会触发layoutSubviews[错误,不用滚动就会触发]
//    [self test_4];
    
    // 5、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
    [self test_5];
}


- (void)test_1
{
    /* 
     解释:
     
     走了initWithFrame:方法,但是又有frame值为{{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.bounds;
    CGFloat height = rect.size.height;
    CGFloat width  = rect.size.width;
    
    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 + 20,
                                  arc4random()%100 + 20,
                                  arc4random()%100 + 20,
                                  arc4random()%100 + 20);
    NSLog(@"_smallView %@", _smallView);
    NSLog(@"_smallView %@", _largeView);
}

@end

测试后的结论是这样子的:

1. 一个view是不能够自己调用layoutSubviews,如果要调用,需要调用setNeedsLayout或者layoutIfNeeded

2. 如果view的frame值为0,即使被添加了耶不会调用layoutSubviews

3. 如果一个view的frame值改变了,那么它的父类的layoutSubviews也会被执行

目录
相关文章
|
1月前
|
算法 Java 大数据
【揭秘】RecursiveAction全面解析
RecursiveAction是Java中一个强大的工具,它允许将复杂任务分解为更小的子任务,这些子任务可以并行执行,从而提高整体性能,其主要优点在于能够有效地利用多核处理器,减少任务执行时间,并简化并行编程的复杂性。
【揭秘】RecursiveAction全面解析
|
1月前
|
并行计算 算法 Java
【揭秘】ForkJoinTask全面解析
ForkJoinTask的显著优点在于其高效的并行处理能力,它能够将复杂任务拆分成多个子任务,并利用多核处理器同时执行,从而显著提升计算性能,此外,ForkJoinTask还提供了简洁的API和强大的任务管理机制,使得开发者能够更轻松地编写并行化代码,高效地利用系统资源。
【揭秘】ForkJoinTask全面解析
|
4月前
VIN解析
VIN解析
41 1
|
9月前
|
存储 算法
Manacher算法解析
Manacher算法解析
57 0
|
11月前
|
编解码
QLED与UHD有何不同?全面解析
QLED和UHD电视几乎是一样的。作为一项先进技术,QLED 已被修改为 4k 分辨率及更高分辨率,因此它们显示为 UHD 显示器。因此,UHD 和 QLED 并不是相互竞争的术语。你应该为你的家买什么取决于你的预算和喜好。
|
自然语言处理 JavaScript 前端开发
这个this的解析
首先得理解this是在函数被调用的时候绑定的,完全取决于函数的调用位置。这与静态作用域相反,反而有点类似动态作用域(由运行时决定)。this在运行时才进行对象绑定。 其次,就是我们常见的this指向问题,即this的绑定。
136 0
|
域名解析 存储 网络协议
阿里云解析是什么?有什么用?
阿里云解析是阿里云为全网域名开放的域名智能解析服务,是一种安全、快速、稳定、可靠的权威DNS解析管理服务。云解析DNS为企业和开发者将易于管理识别的域名转换为计算机用于互连通信的数字IP地址,从而将用户的访问路由到相应的网站或应用服务器。
1987 0
阿里云解析是什么?有什么用?
|
数据采集 存储 索引
Xpath解析
爬虫系列文章
163 0
|
Cloud Native Serverless 开发者
Sequeue 解析
在实际的开发中我们会经常遇到将一条数据需要经过多次处理的场景,称为 Pipeline。那么在 Knative 中是否也提供这样的能力呢?其实从 Knative Eventing 0.7 版本开始,提供了 Sequence CRD 资源,用于事件处理 Pipeline。下面我们介绍一下 Sequence。
1027 0
Sequeue 解析