How can I set the cornerRadius of a UIStackView?
Asked Answered
F

6

32

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.

Fastness answered 25/11, 2015 at 23:0 Comment(1)
Can you share the code before & after? It will be easier to help if we have a snippet of code to look atSorbian
I
78

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.

Isacco answered 26/11, 2015 at 1:9 Comment(2)
or above the stackviewCapsaicin
Why would you put it above the stackview? Wouldn't that cover the stackView content?Galaxy
S
14

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
    }
}
Saimon answered 9/6, 2019 at 21:35 Comment(0)
T
12

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.

Turaco answered 24/5, 2018 at 6:44 Comment(0)
B
4

If you want to provide backgroundColor, CornerRadius and Shadow to StackView:

extension UIStackView {
func insertCustomizedViewIntoStack(background: UIColor, cornerRadius: CGFloat, shadowColor: CGColor, shadowOpacity: Float, shadowRadius: CGFloat) {
        let subView = UIView(frame: bounds)
        subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        subView.layer.cornerRadius = cornerRadius
        subView.backgroundColor = background
        subView.layer.shadowColor = shadowColor
        subView.layer.shadowOpacity = shadowOpacity
        subView.layer.shadowOffset = .zero
        subView.layer.shadowRadius = shadowRadius
        insertSubview(subView, at: 0)
    }
}

If you want to provide backgroundColor, CornerRadius , borderColor and border width to StackView:

extension UIStackView {
     func insertViewIntoStack(background: UIColor, cornerRadius: CGFloat, borderColor: CGColor, borderWidth: CGFloat) {
        let subView = UIView(frame: bounds)
        subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        subView.layer.cornerRadius = cornerRadius
        subView.backgroundColor = background
        subView.layer.borderColor = borderColor
        subView.layer.borderWidth = borderWidth
        insertSubview(subView, at: 0)
    }
    }
Benitobenjamen answered 13/3, 2020 at 13:13 Comment(0)
D
1

If you are in Interface Builder, you can do the following:

  1. Embed your StackView in a View. (Menu -> Editor -> Embed -> View)

  2. Add constraints so that the StackView is constrained exactly to the new superView.

Be careful! xCode is very misleading because if you are looking at the Navigator (on the left) where it shows your constraints, you can think that "bottom = stackView.bottom" means that they are exactly aligned. But no! xCode uses a Standard constraint by Default. You see it in the display are of your xib graphically, but it's subtle and confusing. Look at the Inspector (on the right) where the details are for the constraint, and you will see that the constant is "Standard". Change that to 0!

  1. Checkmark clipToBounds in the View. (Inspector, towards the bottom)

  2. Add the corner radius you want to the View. (Inspector, towards the top)

The answers here that use subView are problematic if your StackView is dynamically sized at run time. If you use a superview (View) with constraints connecting it to the StackView this is not a problem.

Also pay attention to background color in your superview View if that's a factor in the particular layout you are doing. Also shadow, as is mentioned in the other answers here.

Dragnet answered 10/1, 2021 at 20:48 Comment(0)
J
1

It works for me if I, let's say, set

stackView.layer.cornerRadius=5

as long as setting backgroundColor for subviews to .clear

In my case stackView's backgroundColor takes responsibility for background color of inner views, so it fits me.

Jacobin answered 11/2, 2021 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.