Ios 如何设置视图的拐角半径?
我正在更新我的应用程序以使用UIStackViews,现在大多数人都应该升级到iOS 9 在旧版本中,我创建了一个包含两个UIExtFields的UIView,并设置了layer.cornerRadius属性Ios 如何设置视图的拐角半径?,ios,swift,cornerradius,uistackview,Ios,Swift,Cornerradius,Uistackview,我正在更新我的应用程序以使用UIStackViews,现在大多数人都应该升级到iOS 9 在旧版本中,我创建了一个包含两个UIExtFields的UIView,并设置了layer.cornerRadius属性 在新版本中,我创建了一个UIStackView,它由相同的两个UITextFields组成,而不是UIView。当我尝试设置其layer.cornerRadius属性时,似乎什么都没有发生。文档似乎没有任何有用的/相关的信息。UIStackView只管理其排列视图的位置和大小,而corne
在新版本中,我创建了一个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中,则可以执行以下操作:
如果superview视图中的背景色是您正在进行的特定布局中的一个因素,那么也要注意它。还有阴影,如其他答案中所述。如果我,比方说,设置 stackView.layer.cornerRadius=5 只要将子视图的backgroundColor设置为.clear即可
在我的例子中,stackView的backgroundColor负责内部视图的背景色,因此它适合我。您能在前后共享代码吗?如果我们有一段代码可以在stackview上方查看,那么会更容易提供帮助。为什么要将其放在stackview上方?这不包括stackView内容吗?