http://liuxing8807.blog.163.com/blog/static/9703530520134381526554/

sizeThatFits and sizeToFit是UIView的两个方法, 官方文档上说:
- (CGSize)sizeThatFits:(CGSize)size;
作用:return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (void)sizeToFit;
作用: calls sizeThatFits: with current view bounds and changes bounds size. - (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
view.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 0, 0)];
[label setFont:[UIFont systemFontOfSize:20]];
label.text = @"hello wdszgrf";
CGSize sizeThatFits = [label sizeThatFits:CGSizeZero];
NSLog(@"---- %f %f ----", sizeThatFits.width, sizeThatFits.height);
// output: ---- 117.000000 24.000000 ---- NSLog(@"**** %f %f ****", label.frame.size.width, label.frame.size.height);
// output: **** 0.000000 0.000000 **** 说明sizeThatSize并没有改变原始label的大小 [label sizeToFit]; // 这样搞就直接改变了这个label的宽和高,使它依据上面字符串的大小做合适的改变
[label setCenter:CGPointMake(80, 50)];
NSLog(@"==== %f %f ====", label.frame.size.width, label.frame.size.height);
// output: ==== 117.000000 24.000000 ==== [view addSubview:label];
[self.view addSubview:view];
}

sizeThatFits and sizeToFit的更多相关文章

  1. iOS--碎片知识锦集

    知识锦集day01 1.UIView的两个方法: sizeThatFits和 sizeToFit 官方文档上说: - (CGSize)sizeThatFits:(CGSize)size;     // ...

  2. 深入理解Auto Layout 第一弹

    本文转载至 http://zhangbuhuai.com/2015/07/16/beginning-auto-layout-part-1/ By 张不坏 2015-07-16 更新日期:2015-07 ...

  3. iOS - 布局重绘机制相关方法的研究

    iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...

  4. UIButton的探秘

    原文链接 sizeToFit()和sizeThatFits(_:) sizeToFit()会调用sizeThatFits(_:)方法,将现在的frame作为参数.然后根据函数返回的结果更新view. ...

  5. ios UIView sizeToFit sizeThatFits

    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)]; testLabel.backgroundC ...

  6. iOS技术篇:sizeToFit 和 sizeThatFits 区别

    sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...

  7. 转:UIView的sizeToFit与sizeThatFits

    UILabel经常用到的方法- (void)sizeToFit- (CGSize)sizeThatFits:(CGSize)size解释如下: sizeToFit会自动调用sizeThatFits方法 ...

  8. sizeToFit & sizeThatFits

    [sizeToFit & sizeThatFits] 1.sizeToFit,根据sizeThatFits方法返回的大小来调整receiver的大小.自定义子类不应该覆盖这个方法. 2.siz ...

  9. 重写UILabler的sizeThatFits方法,需要触发两次才会有效果

    #import "ViewController.h" @interface SpecialLabel:UILabel @end @implementation SpecialLab ...

随机推荐

  1. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  2. 解决FormClosing事件点击关闭2次的问题

    以下代码:提示框会跳出2遍  private void mFrmmain_FormClosing(object sender, FormClosingEventArgs e) { if (Dialog ...

  3. const学习(续)

    续接上一篇<C++ const学习> const与成员函数 之前说到了const修饰成员函数本身. const成员函数不能修改对象成员值 对于const或者费const对象都可以调用con ...

  4. can't set blob value on that column

    MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...

  5. WordPress熊掌号页面改造,自动发布

    写在前面的话: 有很多小伙伴刚进入WordPress,对很多东西还不太了解,比如:有的主题很挑剔,对于有些插件不兼容,但是呢对于这个功能有不可或缺.所以,这时候就需要我们自己手动修改或者添加代码,来实 ...

  6. Rxjava1升级Rxjava2踩坑一记

    Rxjava1升级Rxjava2坑 共存问题 通常情况下,如果我们希望在一个模块中既想使用rxjava1又想使用rxjava2,这个时候在运行的时候会出现一下报错: ... APK META/-INF ...

  7. Linux学习日记之crontab使用notify-send实现每小时通知提醒

    crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行 通过crontab -e 可以打开编辑文件添加新的命令 notif ...

  8. MAMP中Python安装MySQLdb

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Py ...

  9. zencart,分类,所有,显示

    改的文件:includes/classes/category_tree.php,最简单的修改 if (!isset($first_element)) {        $first_element = ...

  10. RabbitMQ - Publisher的消息确认机制

    queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consumer处理呢?毕竟对于 ...