Junior — Middle
What methods and approaches do you know for rounding or changing the shape of a displayed element in the interface?
sobes.tech AI
Answer from AI
To change the shape of a displayed element in iOS, you can use several approaches:
layer.cornerRadiusproperty — sets the corner radius of a UIView.- Masks with
UIBezierPathandCAShapeLayer— allow creating complex shapes and cutouts. - Using
clipsToBounds = true— so that content does not go outside the rounded borders. - Automatic masks in Interface Builder — configuring corner radii and shadows.
Example of rounding the corners of a UIView:
myView.layer.cornerRadius = 10
myView.clipsToBounds = true
For more complex shapes, you can create a mask:
let path = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10))
let mask = CAShapeLayer()
mask.path = path.cgPath
myView.layer.mask = mask