很多像这样的问题解释了如何编程创建蒙版并将其提供给UIView以实现圆角。
有没有一种方法可以在Storyboard中完成所有操作?只是问一下,因为在Storyboard中创建圆角似乎可以更清晰地划分呈现和逻辑之间的界限。
很多像这样的问题解释了如何编程创建蒙版并将其提供给UIView以实现圆角。
有没有一种方法可以在Storyboard中完成所有操作?只是问一下,因为在Storyboard中创建圆角似乎可以更清晰地划分呈现和逻辑之间的界限。
你可以在故事板中使用用户定义的属性来完成。选择要圆角化的视图并打开其“标识检查器”。在用户定义的运行时属性部分中,添加以下两个条目:
layer.cornerRadius
,类型:数字,值:(你想要的任何半径)layer.masksToBounds
,类型:布尔,值:选中你可能需要在视图的相应类文件中导入QuartzKit
,但我发誓我已经成功地做到了这一点而没有这样做。你的结果可能有所不同。
编辑:动态半径的示例
extension UIView {
/// The ratio (from 0.0 to 1.0, inclusive) of the view's corner radius
/// to its width. For example, a 50% radius would be specified with
/// `cornerRadiusRatio = 0.5`.
@IBDesignable public var cornerRadiusRatio: CGFloat {
get {
return layer.cornerRadius / frame.width
}
set {
// Make sure that it's between 0.0 and 1.0. If not, restrict it
// to that range.
let normalizedRatio = max(0.0, min(1.0, newValue))
layer.cornerRadius = frame.width * normalizedRatio
}
}
}
我验证过这在游乐场中可行。
使用 IBInspectable
将属性添加到 Storyboard 中:
extension UIView {
@IBInspectable var cornerRadiusV: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidthV: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColorV: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}
您需要将视图连接到您的代码文件,然后
yourView.layer.cornerRadius = yourView.frame.width / 2
yourView.layer.masksToBounds = true
即使在Storyboard中进行了所有更改,Woraphot的答案对我仍然无效。
layer.cornerRadius = 10
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor
长答案:
customUIView.layer.cornerRadius = 10
pcustomUIView.layer.borderWidth = 1
customUIView.layer.borderColor = UIColor.blue.cgColor