iPhone UIButton - image position

前端 未结 16 583
梦谈多话
梦谈多话 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 06:51

    Forcing 'right-to-left' for the button is not an option if your app supports both 'left-to-right' and 'right-to-left'.

    The solution that worked for me is a subclass that can be added to the button in the Storyboard and works well with constraints (tested in iOS 11):

    class ButtonWithImageAtEnd: UIButton {
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            if let imageView = imageView, let titleLabel = titleLabel {
                let padding: CGFloat = 15
                imageEdgeInsets = UIEdgeInsets(top: 5, left: titleLabel.frame.size.width+padding, bottom: 5, right: -titleLabel.frame.size.width-padding)
                titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageView.frame.width, bottom: 0, right: imageView.frame.width)
            }
    
        }
    
    }
    

    Where 'padding' would be the space between the title and the image.

提交回复
热议问题