美文网首页
自定义按钮之图片在上文字在下

自定义按钮之图片在上文字在下

作者: 紫金飞侠雷 | 来源:发表于2017-01-22 18:38 被阅读61次

    iOS 系统自定义的 UIButton是默认图片在左边文字在右边,但是有的时候我们在开发中会遇到形形色色的按钮,总结起来说,改变按钮中文字与图片的位置只需要重写 UIButton中的 imageRectForContentRect 方法 和 titleRectForContentRect方法。下边以我自定义的图片在上文字在下按钮为例,直接上代码

    class CLButton: UIButton {

    //所定义的图片高度占按钮高度的比例

    var IWTabBarButtonImageRatio :CGFloat = 0

    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {

    let imageW = contentRect.size.width

    let imageH = contentRect.size.height * IWTabBarButtonImageRatio

    return CGRectMake(0, 0, imageW, imageH)

    }

    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {

    let titleY = contentRect.size.height * IWTabBarButtonImageRatio;

    let titleW = contentRect.size.width;

    let titleH = contentRect.size.height - titleY;

    return CGRectMake(0, titleY, titleW, titleH);

    }

    }

    相关文章

      网友评论

          本文标题:自定义按钮之图片在上文字在下

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