我正在为UIButton
创建自定义类以增加点按区域。我需要用户可以在故事板中输入填充。所以我创建了一个IBInspectable
属性。
@IBDesignable class UIIconButton: UIButton {
@IBInspectable var touchPadding:UIEdgeInsets = UIEdgeInsetsZero
}
但它没有在故事板中显示。但是,如果用CGRect
替换它,那么它在故事板中可见。
答案 0 :(得分:21)
从Xcode 7开始,Interface Builder不会将UIEdgeInsetsZero
理解为@IBInspectable
(无赖!)。
但是,您可以通过使用CGFloat
属性间接设置边缘插入来解决此问题:
public class UIIconButton: UIButton {
@IBInspectable public var bottomInset: CGFloat {
get { return touchPadding.bottom }
set { touchPadding.bottom = newValue }
}
@IBInspectable public var leftInset: CGFloat {
get { return touchPadding.left }
set { touchPadding.left = newValue }
}
@IBInspectable public var rightInset: CGFloat {
get { return touchPadding.right }
set { touchPadding.right = newValue }
}
@IBInspectable public var topInset: CGFloat {
get { return touchPadding.top }
set { touchPadding.top = newValue }
}
public var touchPadding = UIEdgeInsetsZero
}
这将在“界面”构建器中正确显示bottomInset
,leftInset
等。