Tôi tìm thấy nhiều giải pháp ở đây nhưng không cho SWIFT, và tôi biết bạn có thể làm điều này với UIImageView, nhưng trong trường hợp của tôi, tôi cần một hình nền trong suốt có lập trình alpha cho UIButton. Tốt nhất sẽ là một phần mở rộng UIImage cho SWIFT!Cách đặt alpha của UIImage trong SWIFT theo lập trình?
let img = UIImage(named: "imageWithoutAlpha")
var imgInsets = UIEdgeInsetsMake(0, 24, 0, 24)
image = image!.resizableImageWithCapInsets(imgInsets)
let myButton = UIButton(frame: CGRect(x: 50, y: 50, width: img!.size.width, height: img!.size.height))
myButton.setBackgroundImage(img, forState: UIControlState.Normal)
myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 20)
myButton.setTitle("Startbutton", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
myButton.sizeToFit()
view.addSubview(myButton)
kết quả hiện tại:
mong muốn kết quả:
Trong nhanh chóng 2 tôi đã phải thay kCGBlendModeMultiply với CGBlendMode.Multiply, xem http://stackoverflow.com/questions/31309521/error-use-of-unresolved-identifier-kcgblendmodemultiply – ChucK
bạn có thể đăng phiên bản 3 nhanh không? – Patonz
swift 3 phiên bản được thêm vào! –