I'm updating my app to use UIStackViews, now that most people should have updated to iOS 9.
In the old version, I made a UIView consisting of two UITextFields and set its layer.cornerRadius property.
In the new version, I created a UIStackView consisting of the same two UITextFields, instead of the UIView. When I try setting its layer.cornerRadius property nothing seems to happen. The documentation doesn't seem to have any helpful/relevant information.
UIStackView
just manages the position and size of its arranged views, the cornerRadius has no effect. Try to add a custom view below the stackView and set the cornerRadius of it.
add cornerRadius on stackview's superview and enable clipsToBounds property of the superview so that the subviews are confined to the bounds of the superview.
You can use an extension like this:
extension UIStackView {
func customize(backgroundColor: UIColor = .clear, radiusSize: CGFloat = 0) {
let subView = UIView(frame: bounds)
subView.backgroundColor = backgroundColor
subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
insertSubview(subView, at: 0)
subView.layer.cornerRadius = radiusSize
subView.layer.masksToBounds = true
subView.clipsToBounds = true
}
}
来源:https://stackoverflow.com/questions/33927914/how-can-i-set-the-cornerradius-of-a-uistackview