What type of value is [.layerMaxXMinYCorner, .layerMinXMinYCorner]
? Is it possible to set this parameter on a View in Interface Builder? I know how to set layer.borderWidth
, layer.borderUIColor
and layer.cornerRadius
in the Identity Inspector, but I can't figure out the right Type and Value to use for masked corners.
Thanks!
Jake
Update: here are the integer values for each combination (in terms of which are rounded):
Bottom right is in 8-15
Both top corners are in 3, 7, 11, 15
maskedCorners
is a CACornerMask, which is an OptionSet, or bit mask. The raw value is an integer: in code, you can try printing the value of someView.layer.maskedCorners.rawValue
, or setting it via someView.layer.maskedCorners.setValue(3, forKey: "maskedCorners")
.
So you should be able to set the value of layer.maskedCorners
to the integer 3 (or whatever you need) in Interface Builder, and I don't see why it would be unsafe to do this. Though it will be a pain to figure out what set of corners that integer value actually maps to if you forget.
I've just randomly bumped into this same "issue" and wrote another solution, using an extension (no subclassing) and visual toggles in IB for each corner (without setting / adding the bit flags manually).
Keep in mind that the default settings are On / true
(so that "cornerRadius" affects all corners of the view). If you want to round all corners except one, just set that particular corner to false
.
import UIKit
@IBDesignable
public extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
@IBInspectable var topLeft: Bool {
get { return layer.maskedCorners.contains(.layerMinXMinYCorner) }
set {
if newValue {
layer.maskedCorners.insert(.layerMinXMinYCorner)
} else {
layer.maskedCorners.remove(.layerMinXMinYCorner)
}
}
}
@IBInspectable var topRight: Bool {
get { return layer.maskedCorners.contains(.layerMaxXMinYCorner) }
set {
if newValue {
layer.maskedCorners.insert(.layerMaxXMinYCorner)
} else {
layer.maskedCorners.remove(.layerMaxXMinYCorner)
}
}
}
@IBInspectable var bottomLeft: Bool {
get { return layer.maskedCorners.contains(.layerMinXMaxYCorner) }
set {
if newValue {
layer.maskedCorners.insert(.layerMinXMaxYCorner)
} else {
layer.maskedCorners.remove(.layerMinXMaxYCorner)
}
}
}
@IBInspectable var bottomRight: Bool {
get { return layer.maskedCorners.contains(.layerMaxXMaxYCorner) }
set {
if newValue {
layer.maskedCorners.insert(.layerMaxXMaxYCorner)
} else {
layer.maskedCorners.remove(.layerMaxXMaxYCorner)
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With