Button to find out which of two image attributes:
Image: Set button images will overwrite the text inside
Background: Set button background image, the text inside still display
Most real development, we set the button in the main image
, more convenient way to set the picture contentMode.
let btn = UIButton() //或是storyboard上拉过来的button的outlet
//...
btn.setImage(UIImage(named: "xxx"), for: .normal) //一般状态时的图片
btn.setImage(UIImage(named: "xxx"), for: .highlighted) //按钮被touch之后的图片
btn.imageView?.contentMode = .scaleAspectFit
复制代码
Advertising time: brother iOS12 zero-based video tutorials (each chapter Jieke audition):
Reproduced in: https: //juejin.im/post/5d0717455188254a10112810