gpt4 book ai didi

ios - 如何将 Storyboard UIView 设置为 View 的子类?

转载 作者:行者123 更新时间:2023-11-28 13:07:06 26 4
gpt4 key购买 nike

我添加了 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)
}
}

然后我有一个 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)
}
}

我在 Storyboard中添加了一个 UIView,并将类设置为 WhiteWithGreyTBBorderView。它仍然是白色而不是黑色,在我得到的控制台中:

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.

知道哪里出了问题吗?

最佳答案

当您使用 Storyboard 时, View 是通过调用 init(coder aDecoder: NSCoder) 创建的。您需要做的就是在所有初始化方法中调用 addBehavior :)
这样您就可以确保在所有情况下都正确配置了您的 View (当您从代码和 Storyboard创建它时)。就是这样

你可以设置一个断点,当你从 Storyboard 或 xib 实例化一个 View 时,你会看到 init (frame : CGRect) 没有被调用。
:)

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

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

关于ios - 如何将 Storyboard UIView 设置为 View 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com