Swift设置图片在Button的位置(上下左右及间距)

import UIKit

extension UIButton {

    /** 图片在左,标题在右 */

    func setIconInLeft() {

        setIconInLeftWithSpacing(0)

    }

    /** 图片在右,标题在左 */

    func setIconInRight() {

        setIconInRightWithSpacing(0)

    }

    /** 图片在上,标题在下 */

    func setIconInTop() {

        setIconInTopWithSpacing(0)

    }

    /** 图片在下,标题在上 */

    func setIconInBottom() {

        setIconInBottomWithSpacing(0)

    }

    //** 可以自定义图片和标题间的间隔 */

    func setIconInLeftWithSpacing(_ Spacing: CGFloat) {

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = 0

        titleEdgeInsets.`left` = 0

        titleEdgeInsets.bottom = 0

        titleEdgeInsets.`right` = 0

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = 0

        imageEdgeInsets.`left` = 0

        imageEdgeInsets.bottom = 0

        imageEdgeInsets.`right` = 0

    }

    func setIconInRightWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = 0

        titleEdgeInsets.`left` = -(img_W + Spacing / 2)

        titleEdgeInsets.bottom = 0

        titleEdgeInsets.`right` = img_W + Spacing / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = 0

        imageEdgeInsets.`left` = tit_W + Spacing / 2

        imageEdgeInsets.bottom = 0

        imageEdgeInsets.`right` = -(tit_W + Spacing / 2)

    }

    func setIconInTopWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let img_H: CGFloat? = imageView?.frame.size.height

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        let tit_H: CGFloat? = titleLabel?.frame.size.height

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = tit_H / 2 + Spacing / 2

        titleEdgeInsets.`left` = -(img_W / 2)

        titleEdgeInsets.bottom = -(tit_H / 2 + Spacing / 2)

        titleEdgeInsets.`right` = img_W / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = -(img_H / 2 + Spacing / 2)

        imageEdgeInsets.`left` = tit_W / 2

        imageEdgeInsets.bottom = img_H / 2 + Spacing / 2

        imageEdgeInsets.`right` = -(tit_W / 2)

    }

    func setIconInBottomWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let img_H: CGFloat? = imageView?.frame.size.height

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        let tit_H: CGFloat? = titleLabel?.frame.size.height

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = -(tit_H / 2 + Spacing / 2)

        titleEdgeInsets.`left` = -(img_W / 2)

        titleEdgeInsets.bottom = tit_H / 2 + Spacing / 2

        titleEdgeInsets.`right` = img_W / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = img_H / 2 + Spacing / 2

        imageEdgeInsets.`left` = tit_W / 2

        imageEdgeInsets.bottom = -(img_H / 2 + Spacing / 2)

        imageEdgeInsets.`right` = -(tit_W / 2)

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容