web-dev-qa-db-ja.com

UIStackViewのcornerRadiusを設定するにはどうすればよいですか?

UIStackViewsを使用するようにアプリを更新しています。ほとんどの人がiOS 9に更新しているはずです。

古いバージョンでは、2つのUITextFieldsで構成されるUIViewを作成し、そのlayer.cornerRadiusプロパティを設定しました。

新しいバージョンでは、UIViewではなく、同じ2つのUITextFieldで構成されるUIStackViewを作成しました。 layer.cornerRadiusプロパティを設定しようとしても、何も起こらないようです。ドキュメントに役立つ/関連する情報がないようです。

21
WallyAmerica

UIStackViewは、配置されたビューの位置とサイズを管理するだけで、cornerRadiusは効果がありません。 stackViewの下にカスタムビューを追加して、そのcornerRadiusを設定してみてください。

53
ZHZ

スタックビューのスーパービューにcornerRadiusを追加し、スーパービューのclipsToBoundsプロパティを有効にして、サブビューがスーパービューの境界に限定されるようにします。

4
tahmina

次のような拡張機能を使用できます。

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
    }
}
1
Murat Yasar

StackColorにbackgroundColor、CornerRadius、Shadowを提供する場合:

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 = backgroundColor
        subView.layer.shadowColor = shadowColor
        subView.layer.shadowOpacity = shadowOpacity
        subView.layer.shadowOffset = .zero
        subView.layer.shadowRadius = shadowRadius
        insertSubview(subView, at: 0)
    }
}

StackViewにbackgroundColor、CornerRadius、borderColor、およびボーダーの幅を提供する場合:

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 = backgroundColor
        subView.layer.borderColor = borderColor
        subView.layer.borderWidth = borderWidth
        insertSubview(subView, at: 0)
    }
    }
0
Shankar