解析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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux命令行修改用户名_linux 更改用户密码

    linux命令行修改用户名_linux 更改用户密码一、《Linux的chmod命令》。在shell中,可以使用chown命令来改变文件所有者及用户组,chgrp命令来改变文件所在用户组。在Linux的C程序中,可以使用chown函数来改变文件所有者,及所在用户组。另外,在shell中,要修改文件当前的用户必须具有管理员root的权限。可以通过su命令切换到root用户,也可以通过sudo获得root的权限。二、使用chown命令更改文件拥有…

    2022年9月17日
    0
  • C语言基础:哪儿来的32766

    这篇文章通过一个简单的示例程序来介绍一下如何结合使用IDE来学习C语言。

    2022年4月4日
    48
  • php layer弹出层更改背景,详解Layer弹出层样式[通俗易懂]

    php layer弹出层更改背景,详解Layer弹出层样式[通俗易懂]前言:学习layer弹出框,之前项目是用bootstrap模态框,后来改用layer弹出框,在文章的后面,我会分享项目的一些代码(我自己写的)。layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护、不断建设和提升社区服务,使得猿们纷纷自发传播,乃至于成为今天的Layui最强劲的源动力。目前,layer已成为国内最多人使用的web弹层组件,GitHub自然…

    2022年7月13日
    13
  • 微信小程序使用MQTT.js连接阿里云IoT物联网平台[通俗易懂]

    微信小程序使用MQTT.js连接阿里云IoT物联网平台[通俗易懂]前言最近公司要做物联网控制,觉得写app不能够兼容Android和iOS,于是选定了微信小程序来作为控制端,为了能够实时的监听到设备的状态变化,需要服务器能够主动推消息给小程序,一开始考虑了websocket,由服务器进行上报的数据监听,小程序使用websocket连接服务器接收消息,虽然能实现,但是加上业务逻辑之后就太复杂终归不好(因为服务器也是自己写–泪奔)于是想着,websocket既然…………………

    2022年8月31日
    8
  • 分布式——CAP原理

    分布式——CAP原理一.概述在理论计算机科学中,CAP原理指出对于一个分布式系统来说,当设计读写操作时,只能同时满足一下三点中两个:一致性(Consistence):所有节点访问同一份最新的数据副本 可用性(Avaliability):非故障的节点在合理时间内返回合理的响应(不是错误或者超时的响应) 分区容错性(Partitiontolerance):分布式系统出现网络分区(分布式系统中,多个节点之前的网络本来是连通的,但是由于某些故障,比如部分节点网络出了问题。某些节点之间不连通,整个网络就分为几个区域,这就叫

    2022年5月19日
    46
  • Ubuntu安装五笔输入法「建议收藏」

    Ubuntu安装五笔输入法「建议收藏」学习Ubuntu 环境VirtualBox 准备好Ubuntu系统后,这里下载的是12.04LTS版本1.安装五笔输入法在网上找资料,通过Ibus平台安装五笔输入法发现本操作系统已安装了Ibus,然后直接安装五笔IBUS五笔:sudoapt-getinstallibus-table-wubi2.设置输入法ibus-setup

    2022年7月26日
    2

发表回复

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

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