How can I set the cornerRadius of a UIStackView?

前端 未结 4 385
無奈伤痛
無奈伤痛 2020-12-15 17:50

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

4条回答
  •  太阳男子
    2020-12-15 18:17

    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)
        }
        }
    

提交回复
热议问题