解析LayoutSubviews

解析LayoutSubviews解析LayoutSubviews原博:http://www.cnblogs.com/YouXianMing/p/3897543.html从百度上搜索了一下layoutSubviews的用处,以下是搜索的结果,当然,笔者是会一一验证的.1、init初始化不会触发layoutSubviews2、addSubview会触发layoutSubviews3、设置vie

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

解析LayoutSubviews

原博:http://www.cnblogs.com/YouXianMing/p/3897543.html

从百度上搜索了一下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也会被执行

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159925.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机夏令营、预推免面试经验【北理cs,北航cs,复旦cs大数据,浙大cs】「建议收藏」

    计算机夏令营、预推免面试经验【北理cs,北航cs,复旦cs大数据,浙大cs】「建议收藏」这是一篇非寻常经验贴,因为今年疫情影响下的保研实在是太奇怪了,这里主要说说我了解到的一些学校的一些特点,可以做参考首先推荐一个计算机保研群,和各大机构的不一样,里边全是历届保研的同学们,大家都知道的很多,在这个群里我真的学到了不少,群主虎哥也很nice个人状况末流211物联网工程专业1/64夏令营入营:南方科技大学(优营)听说这个学校很有钱,这里有一个很好的老师,好像是搞,如果是冲老师去的可以报,但知名度实在不高,也没有牌子,自己权衡吧南开人工智能学院(杰出营员):感谢南开ai,是我梦开始

    2022年7月25日
    120
  • 在智联上投了一个月的简历,很多都有意向,但是却没有通知我去

    在智联上投了一个月的简历,很多都有意向,但是却没有通知我去

    2022年2月13日
    219
  • scrapy爬虫部署服务器「建议收藏」

    scrapy爬虫部署服务器「建议收藏」scrapy爬虫部部署服务器时间:2020年5月27日18:28:30作者:钟健记录:scrapy爬虫关键字:scrapyscrapydscrapydweb一、scrapy爬虫部署服务器scrapy通过命令行运行一般只用于测试环境,而用于运用在生产环境则一般都部署在服务器中进行远程操作。scrapy部署服务器有一套完整的开源项目:scrapy+scrapyd(服务端)+scrapy-client(客户端)+scrapydweb1、scrapyd1.介绍Scrapyd是用于部署和运

    2022年6月18日
    28
  • SVN——强大的版本管理系统[通俗易懂]

    SVN——强大的版本管理系统[通俗易懂]SVN——强大的版本管理系统

    2022年4月24日
    50
  • CriticalSection_protection initialization

    CriticalSection_protection initialization如果EnterCriticalSection将一个线程置于等待状态,那么该线程在很长时间内就不能再次被调度。实际上,在编写得不好的应用程序中,该线程永远不会再次被赋予CPU时间。TryEnterCriticalSection函数决不允许调用线程进入等待状态。它的返回值能够指明调用线程是否能够获得对资源的访问权。TryEnterCriticalSection发现该资源已经被另一个线程访问,它就返回F

    2022年9月20日
    4
  • RPN网络通俗理解

    RPN网络通俗理解【提特征】RPN网络前面是一个提特征的网络,比如VGG,Res等,传给RPN网络的是一个特征图,其实也就是一个tensor比如用ZF网络(论文里面用的)输出特征图:13*13*256【RPN】拿到模型的特征,RPN网络首先加了一个3*3*256*256的卷积层(其实不是很清楚为什么加,可能是为了扩大感受野)这样就会得到11*11*256的输出,前面11*11是图形矩阵,其中每一个点在原图中都是一个…

    2022年6月23日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号