美文网首页
UIButton的探秘

UIButton的探秘

作者: 我是花老虎 | 来源:发表于2016-07-10 23:56 被阅读87次

    原文链接

    sizeToFit()和sizeThatFits(_:)

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

    sizeToFit will simply call through to sizeThatFits: passing the view's current size as the argument. It will then update the view's frame based on the value it gets back. So all the important logic goes in sizeThatFits:, and this is the method you should override for your own custom controls.

    UIButton标题的shadow效果与反转

            myButton?.titleLabel?.shadowOffset = CGSizeMake(2.0, 2.0)
            myButton?.setTitleShadowColor(UIColor.redColor(), forState: .Normal)
    //        myButton?.showsTouchWhenHighlighted = true
            myButton?.reversesTitleShadowWhenHighlighted = true
    
    未选中 高亮态

    加上选中态亮瞎眼的效果

            myButton?.showsTouchWhenHighlighted = true
    
    亮瞎眼

    UIButton的布局与UIEdgeInsets

    有三个UIEdgeInsets

    1. contentEdgeInsets
      使用sizeThatFits(_:)计算时用到。
    2. titleEdgeInsets
      使用sizeThatFits(_:)计算时不会用到。
    3. imageEdgeInsets
      使用sizeThatFits(_:)计算时不会用到。

    You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge.

    举例说明

    • contentEdgeInsets
    contentEdgeInsets
    left和right都是30,会使左边向中间移动30pt,右边向中间移动30pt。也就是左间距和右间距都是30pt。由于contentEdgeInsets参与sizeThatFits(_:)的计算,所以最终表现为整个button被拉宽。
    • titleEdgeInsets
    titleEdgeInsets
    保持contentEdgeInsets不变,设置titleEdgeInsets的left为15。由于titleEdgeInsets在sizeThatFits(_:)中不参与,因此button没有被拉宽,只是titleLabel的左边向右(中心)移动了15pt,右边保持不变。导致字符串没有被显示全。

    设置titleEdgeInsets的right为-15,结果是titleLabel的右边向右移动了15pt,从而整体的显示范围不变。

    • imageEdgeInsets

    图像在右,标题在左

    button.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    

    链接

    相关文章

      网友评论

          本文标题:UIButton的探秘

          本文链接:https://www.haomeiwen.com/subject/hgbdjttx.html