How to change UIButton image in Swift

前端 未结 12 1370
猫巷女王i
猫巷女王i 2020-12-01 00:25

I am trying to change the image of a UIButton using Swift... What should I do

This is OBJ-C code.but I don\'t know with Swift:

[playButton setImage:[         


        
12条回答
  •  余生分开走
    2020-12-01 01:05

    Swift 5 and ensures the image scales to the size of the button but stays within the buttons bounds.

    yourButton.clipsToBounds = true
    yourButton.contentMode = .scaleAspectFill
    
    // Use setBackgroundImage or setImage
    yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)
    

提交回复
热议问题