【发布时间】:2016-03-16 21:09:00
【问题描述】:
很多问题like this 解释了如何以编程方式创建遮罩并为 UIView 提供圆角。
有没有办法在 Storyboard 中完成这一切?只是询问,因为在 Storyboard 中创建圆角似乎可以在表示和逻辑之间保持更清晰的界限。
【问题讨论】:
标签: ios xcode swift storyboard autolayout
很多问题like this 解释了如何以编程方式创建遮罩并为 UIView 提供圆角。
有没有办法在 Storyboard 中完成这一切?只是询问,因为在 Storyboard 中创建圆角似乎可以在表示和逻辑之间保持更清晰的界限。
【问题讨论】:
标签: ios xcode swift storyboard autolayout
【讨论】:
Error: Launch screens may not use user defined runtime attributes. 看起来如果你需要在 LaunchScreen 上显示一个圆形图像,你就别无选择了。
您可以使用用户定义的属性在情节提要中执行此操作。选择要舍入的视图并打开其 Identity Inspector。在 User Defined Runtime Attributes 部分中,添加以下两个条目:
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 将属性添加到情节提要:
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
}
}
}
【讨论】:
即使在情节提要中进行了所有更改后,Woraphot's answer 对我也不起作用。
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
【讨论】: