Objective C Setter overriding in Swift

后端 未结 3 1724
情歌与酒
情歌与酒 2020-12-24 11:36

I need to override the setter of UIViews highlighted property in my custom UIButton subclass ;

Objective C

@property(nonatomic,getter=isHighlighted)          


        
3条回答
  •  旧时难觅i
    2020-12-24 12:01

    In Swift the solution above worked for me, but I had to omit the Bool = true:

    import UIKit
    
    class CustomUIButtonForUIToolbar: UIButton {
    
        // Only override drawRect: if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        override func drawRect(rect: CGRect) {
            // Drawing code
            super.drawRect(rect)
    
            self.layer.borderColor = UIColor.blueColor().CGColor
            self.layer.borderWidth = 1.0
            self.layer.cornerRadius = 5.0
            self.clipsToBounds = true
            self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    
            self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
        }
    
        override var highlighted: Bool {
            didSet {
    
                if (highlighted) {
                    self.backgroundColor = UIColor.blueColor()
                }
                else {
                    self.backgroundColor = UIColor.clearColor()
                }
    
            }
        }
    
    }
    

提交回复
热议问题