我有一个有趣的问题,但是我不知道如何解决它。我试图使用@IBInspectable扩展UIView。然而,使用这种方法,圆角似乎是从nib文件的默认边设置的,而不是视图的实际大小。
因此,当我在IB中将“View as”设置为iPhoneSE并为iPhoneSE构建时,视图是一个圆。然而,如果我为iPhone7构建,则边角没有完全变成圆形。相反,如果我将“View as”设置为iPhone7并为iPhone7构建,则视图是一个圆,但是如果我为iPhoneSE构建,则角落会过度圆润。
以下是图片和代码:
扩展
因此,当我在IB中将“View as”设置为iPhoneSE并为iPhoneSE构建时,视图是一个圆。然而,如果我为iPhone7构建,则边角没有完全变成圆形。相反,如果我将“View as”设置为iPhone7并为iPhone7构建,则视图是一个圆,但是如果我为iPhoneSE构建,则角落会过度圆润。
以下是图片和代码:
扩展
extension UIView {
@IBInspectable var cornerRadius:Double {
get {
return Double(layer.cornerRadius)
}
set {
layer.cornerRadius = CGFloat(newValue)
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var circleView:Bool {
get {
return layer.cornerRadius == min(self.frame.width, self.frame.height) / CGFloat(2.0) ? true : false
}
set {
if newValue {
layer.cornerRadius = min(self.frame.width, self.frame.height) / CGFloat(2.0)
layer.masksToBounds = true
}
else{
layer.cornerRadius = 0.0
layer.masksToBounds = false
}
}
}
}
"View as"设置为iPhone SE在IB中
专为iPhone SE打造
为 iPhone 7 构建
"“视图”设置为iPhone7
建立适用于iPhone SE的应用程序
"适用于iPhone 7