简体   繁体   中英

How to set storyboard UIView to subclass of view?

I've added a subclass of UIView:

class WhiteWithGreyTBBorderView: UIView {
    override init (frame : CGRect) {
        super.init(frame : frame)
        addBehavior()
    }

    convenience init () {
        self.init(frame:CGRectZero)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func addBehavior (){
        backgroundColor = UIColor.blackColor()
        addTopBorderWithColor(UIColor.blackColor(), width: 5)
    }
}

Then I have a ViewExtension:

extension UIView {
    func addTopBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, 0, self.frame.size.width, width)
        self.layer.addSublayer(border)
    }

    func addRightBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(self.frame.size.width - width, 0, width, self.frame.size.height)
        self.layer.addSublayer(border)
    }

    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, self.frame.size.height - width, self.frame.size.width, width)
        self.layer.addSublayer(border)
    }

    func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, 0, width, self.frame.size.height)
        self.layer.addSublayer(border)
    }
}

I added a UIView to my storyboard and set the class to WhiteWithGreyTBBorderView. It's still white and not black, in the console I get:

2015-09-03 14:28:38.802 NewsApp[6801:1954504] Failed to set (borderColor) user defined inspected property on (NewsApp.WhiteWithGreyTBBorderView): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key borderColor. 2015-09-03 14:28:38.803 NewsApp[6801:1954504] Failed to set (borderWidth) user defined inspected property on (NewsApp.WhiteWithGreyTBBorderView): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key borderWidth.

Any idea what can be wrong?

When you use Storyboard, the view is created by calling init(coder aDecoder: NSCoder) . All you need to do is call addBehavior in all init methods :)
This way you ensure that your view is configured correctly in all cases (when you create it from code and from storyboard). That's it

You can set a breakpoints and see that init (frame : CGRect) is not called when you instantiate a view from Storyboard or xib.
:)

convenience init () {
    self.init(frame:CGRectZero)
    addBehavior()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    addBehavior()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM