我用类WWZButton创建了一个按钮,但是这个类显然不起作用。我想添加一个' arrow_btn '
的图像这就是它现在的样子。但是需要在右侧成为arrow_btn。 Login button
这是类代码:`import UIKit
类WWZButton:UIButton {
@IBInspectable var isOrange: Bool = true
private var rightImageView: UIImageView?
func setupView() {
rightImageView?.removeFromSuperview()
rightImageView = UIImageView(frame: CGRect(x: frame.width - 35, y: 7.5, width: 25, height: 15), image: UIImage(named: "arrow_btn")!)
rightImageView?.contentMode = .ScaleAspectFit
addSubview(rightImageView!)
contentHorizontalAlignment = .Left
contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
setTitleColor(UIColor.whiteColor(), forState: .Normal)
Utilities.createBorder(forView: self, color: UIColor.clearColor(), width: 0, radius: 8)
if isOrange == true {
backgroundColor = UIColor.orangeButtonColor()
} else {
backgroundColor = UIColor.grayButtonColor()
}
let heightConstraint = NSLayoutConstraint(item: self,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1,
constant: 30)
self.addConstraint(heightConstraint)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
}
Screenshot of button class + isOrange set true
为什么不起作用?代码很好吗?
答案 0 :(得分:0)
这适用于我
class WWZButton: UIButton {
@IBInspectable var isOrange: Bool = true
private var rightImageView: UIImageView?
func setupView() {
rightImageView?.removeFromSuperview()
rightImageView?.contentMode = .scaleAspectFit
rightImageView = UIImageView(frame: CGRect(x: frame.width - 35, y: 7.5, width: 40, height: 40))
rightImageView?.image = UIImage.init(named: "car1.png")
rightImageView?.contentMode = .scaleAspectFit
addSubview(rightImageView!)
contentHorizontalAlignment = .left
contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
setTitleColor(UIColor.white, for: .normal)
if isOrange == true {
backgroundColor = UIColor.green
} else {
backgroundColor = UIColor.blue
}
let heightConstraint = NSLayoutConstraint(item: self,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 100)
self.addConstraint(heightConstraint)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
}