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. 常用点击事件(鼠标、光标、键盘、body)

    常用事件: 鼠标: onclick(单击) ondblclick(双击) oncontextmenu(右击) onmouseover onmouseout 光标: onfocus onblur 键盘: ...

  2. 双摆模拟 python(转)

    双摆 是 混沌理论 中的一个常见示例,这里通过 python 的作图工具包Matplotlib 来模拟双摆的运动过程: 注意:在 vs2017 中可以正确运行程序,在 cmd 环境下有时报错 &quo ...

  3. js操作Attribute,控件的各种属性.....maxlength,style...

    Attribute是属性的意思,文章仅对部分兼容IE和FF的Attribute相关的介绍. attributes:获取一个属性作为对象 getAttribute:获取某一个属性的值setAttribu ...

  4. Spring Boot (27) actuator服务监控与管理

    actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...

  5. 卸载掉原有mysql

    [root@xiaoluo ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 有的话,我们就通过 rpm -e 命令 或者 rpm - ...

  6. 关于定位中left和right,top和bottom的权重问题

    关于定位中left和right,top和bottom的权重问题 在共同类中设置了定位并且设置了left等定位,如果你引用这个类并加入其他的类中也有left和right等定位,那么你设置的right或是 ...

  7. Framework7首页隐藏navbar

    f7首页隐藏navbar其他页面显示navbar 帮别人解决问题,自己也记录一下, 首页.navbar加.navbar-hidden, 首页.page加.no-navbar, 如果首页有.navbar ...

  8. JS高级——监听浏览器的返回事件

    https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...

  9. 在自学css开始就遇到问题,“链入外部样式表”在多浏览器显示问题

    在自学css开始就遇到问题,“链入外部样式表”的习题,代码如下:A.被链入的CSS文件代码.css<style  type="text/css"><!--h1{b ...

  10. ubuntu+ngnix+thinkphp pathinfo配置

    一.thinkphp 项目改为pathinfo模式 XXX/ThinkPHP/Conf/convention.php文件中找到 'URL_MODEL' => 1, // URL访问模式,可选参数 ...