UIButton的imageEdgeInsets和titleEdgeInsets

参考

https://www.jianshu.com/p/0f34c1b52604

其中案例上下居中不好使,改了一下

    func alignImageAndTitleVertically(spacing: CGFloat = 6.0) {
        let imageSize = self.imageView!.frame.size
        let titleSize = self.titleLabel!.frame.size
        let totalHeight = imageSize.height + titleSize.height + spacing
        let leftMargin = (self.size.width - imageSize.width)/2

        self.imageEdgeInsets = UIEdgeInsets(
            top: -(totalHeight - imageSize.height),
            left: leftMargin,
            bottom: 0,
            right: -leftMargin
        )

        self.titleEdgeInsets = UIEdgeInsets(
            top: 0,
            left: -imageSize.width,
            bottom: -(totalHeight - titleSize.height),
            right: 0
        )

    }

猜你喜欢

转载自blog.csdn.net/linzhiji/article/details/128683532