iPhone UIButton - image position

前端 未结 16 589
梦谈多话
梦谈多话 2020-12-04 06:18

I have a UIButton with text \"Explore the app\" and UIImage (>) In Interface Builder it looks like:

[ (>) Explore t         


        
16条回答
  •  一生所求
    2020-12-04 07:05

    Another simple way (that is NOT iOS 9 only) is to subclass UIButton to override these two methods

    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        var rect = super.titleRectForContentRect(contentRect)
        rect.origin.x = 0
        return rect
    }
    
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        var rect = super.imageRectForContentRect(contentRect)
        rect.origin.x = CGRectGetMaxX(contentRect) - CGRectGetWidth(rect)
        return rect
    }
    

    contentEdgeInsets is already taken into account by using super.

提交回复
热议问题