Ios 如何设置视图的拐角半径?

Ios 如何设置视图的拐角半径?,ios,swift,cornerradius,uistackview,Ios,Swift,Cornerradius,Uistackview,我正在更新我的应用程序以使用UIStackViews,现在大多数人都应该升级到iOS 9 在旧版本中,我创建了一个包含两个UIExtFields的UIView,并设置了layer.cornerRadius属性 在新版本中,我创建了一个UIStackView,它由相同的两个UITextFields组成,而不是UIView。当我尝试设置其layer.cornerRadius属性时,似乎什么都没有发生。文档似乎没有任何有用的/相关的信息。UIStackView只管理其排列视图的位置和大小,而corne

我正在更新我的应用程序以使用UIStackViews,现在大多数人都应该升级到iOS 9

在旧版本中,我创建了一个包含两个UIExtFields的UIView,并设置了layer.cornerRadius属性


在新版本中,我创建了一个UIStackView,它由相同的两个UITextFields组成,而不是UIView。当我尝试设置其layer.cornerRadius属性时,似乎什么都没有发生。文档似乎没有任何有用的/相关的信息。

UIStackView
只管理其排列视图的位置和大小,而cornerRadius没有任何影响。尝试在stackView下方添加自定义视图,并设置其cornerRadius。

在stackView的superview上添加cornerRadius,并启用superview的clipsToBounds属性,以便将子视图限制在superview的边界内。

您可以使用如下扩展:

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

如果要为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)
    }
}
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)
    }
    }

如果要向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)
    }
}
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)
    }
    }

如果您在Interface Builder中,则可以执行以下操作:

  • 将StackView嵌入到视图中。(菜单->编辑器->嵌入->视图)

  • 添加约束,以便将StackView精确约束到新的superView

  • 小心!xCode非常容易引起误解,因为如果您查看显示约束的导航器(在左侧),您可能会认为“bottom=stackView.bottom”表示它们完全对齐。但是没有!默认情况下,xCode使用标准约束。您可以在xib的显示中以图形方式看到它,但它很微妙且令人困惑。查看Inspector(右侧)中约束的详细信息,您将看到常量为“标准”。将其更改为0

  • 选中视图中的clipToBounds。(检查员,朝底部)

  • 将所需的角半径添加到视图中。(检查员,朝上)

  • 如果您的StackView在运行时动态调整大小,那么这里使用子视图的答案是有问题的。如果使用带有约束的superview(视图)将其连接到StackView,则这不是问题


    如果superview视图中的背景色是您正在进行的特定布局中的一个因素,那么也要注意它。还有阴影,如其他答案中所述。

    如果我,比方说,设置

    stackView.layer.cornerRadius=5

    只要将子视图的backgroundColor设置为.clear即可


    在我的例子中,stackView的backgroundColor负责内部视图的背景色,因此它适合我。

    您能在前后共享代码吗?如果我们有一段代码可以在stackview上方查看,那么会更容易提供帮助。为什么要将其放在stackview上方?这不包括stackView内容吗?