【发布时间】:2019-05-30 17:42:37
【问题描述】:
我为此创建了一个自定义类。但它仅适用于左上角,不适用于其他:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false
@IBInspectable public var topRight: Bool = false
@IBInspectable public var bottomLeft: Bool = false
@IBInspectable public var bottomRight: Bool = false
@IBInspectable public var cornerRadius: Int = 0
public override func awakeFromNib() {
var options = UIRectCorner()
if topLeft { options = options.union(.topLeft) }
if topRight { options = options.union(.topRight) }
if bottomLeft { options = options.union(.bottomLeft) }
if bottomRight { options = options.union(.bottomRight) }
let path = UIBezierPath(roundedRect:self.bounds,
byRoundingCorners:options,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}
这是在模拟器中运行时的外观,我为所有 4 个角应用角半径:
这里发生了什么? TopLeft 工作,TopRight 轻微和底角根本没有。我很困惑。
【问题讨论】:
-
layoutIfNeeded 我认为它适用于您的 ui 更新
-
@YogeshPatel - 这将触发
layoutSubviews被调用,所以除非他将代码移到该方法中,否则调用layoutIfNeeded不会做任何事情。而且,顺便说一句,打电话给setNeedsLayout可能更谨慎(如果你要更新多个角落,你不希望重复调用layoutSubviews)。 -
是的哇你太棒了@Rob
标签: ios swift uistoryboard