简体   繁体   English

在 iOS 堆栈视图中水平居中视图

[英]Center views horizontally in iOS stack view

I'm trying to achieve the following layout (views centered horizontally):我正在尝试实现以下布局(视图水平居中):

在此处输入图片说明

So I set up a stack view like so:所以我像这样设置了一个堆栈视图:

let quickPromptsView: UIStackView = {
    let sv = UIStackView()
    sv.axis = .horizontal
    sv.alignment = .center
    sv.distribution = .equalSpacing
    sv.spacing = 10
    sv.translatesAutoresizingMaskIntoConstraints = false
    return sv
}()

Adding buttons to stack views:将按钮添加到堆栈视图:

func addOptions(options: [DialogNodeOutputOptionsElement]) {
    DispatchQueue.main.async {
        // clear all subviews
        self.quickPromptsView.subviews.forEach { (view) in
            view.removeFromSuperview()
        }
        for option in options {
            let button = QuickPromptButton()
            button.setTitle(option.label, for: .normal)
            button.addTarget(self, action: #selector(self.didTapQuickPrompt), for: .touchUpInside)
            self.quickPromptsView.addArrangedSubview(button)
        }
    }
}

Button class:按钮类:

class QuickPromptButton: UIButton {

    var userFacingValue: String?
    var answerValue: String?

    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.borderColor = UIColor.primaryColor.cgColor
        layer.borderWidth = 1
        layer.cornerRadius = 15
        setTitleColor(.primaryColor, for: .normal)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

This is how I add the stack view, I add it inside another stack part of the MessageKit这就是我添加堆栈视图的方式,我将它添加到MessageKit 的另一个堆栈部分中

func configureQuickPromptsView() {
    view.addSubview(quickPromptsView)
    quickPromptsView.heightAnchor.constraint(equalToConstant: 40).isActive = true

    // this stack view belongs to the MessageKit library
    messageInputBar.topStackView.axis = .horizontal
    messageInputBar.topStackView.distribution = .fill
    messageInputBar.topStackView.addArrangedSubview(quickPromptsView)
}

However, this is what I get:但是,这就是我得到的:

在此处输入图片说明

The stack view has 100% the width of the screen.堆栈视图具有 100% 的屏幕宽度。 I've tried every single type of distribution but that didn't work.我已经尝试了每一种类型的distribution但没有奏效。 I've also tried to insert transparent UIView s at the extremes to force the centering but that seems like a hack.我还尝试在极端情况下插入透明的UIView以强制居中,但这似乎是一种黑客攻击。 Any ideas?有任何想法吗?

Just center your stack view horizontally in its superview.只需在其超级视图中水平居中您的堆栈视图。

Examples below are using UIKitPlus library (it is pure UIKit but declarative and support LivePreview, iOS9+)下面的示例使用UIKitPlus库(它是纯 UIKit 但声明性并支持 LivePreview,iOS9+)

UView {
    UHStack {
        UView {
            UText("Yes")
                .color(.green)
                .edgesToSuperview(h: 8, v: 4)
        }
        .border(1, .green)
        .circle()
        UView {
            UText("No")
                .color(.red)
                .edgesToSuperview(h: 8, v: 4)
        }
        .border(1, .red)
        .circle()
        UView {
            UText("I don't know")
                .color(.darkGray)
                .edgesToSuperview(h: 8, v: 4)
        }
        .border(1, .darkGray)
        .circle()
    }
    .alignment(.center)
    .distribution(.equalSpacing)
    .spacing(10)
    .edgesToSuperview(v: 0)
    .centerXInSuperview() // this will center your stack view
}
.edgesToSuperview(h: 0)
.centerYInSuperview()

第一个解决方案

Or use a little hack by adding two views with equal width, one in the beginning of stack and one as the last view in stack.或者通过添加两个宽度相等的视图来使用一个小技巧,一个在堆栈的开头,一个作为堆栈中的最后一个视图。

let v1 = UView()
UHStack {
    v1
    UView {
        UText("Yes")
            .color(.green)
            .edgesToSuperview(h: 8, v: 4)
    }
    .border(1, .green)
    .circle()
    UView {
        UText("No")
            .color(.red)
            .edgesToSuperview(h: 8, v: 4)
    }
    .border(1, .red)
    .circle()
    UView {
        UText("I don't know")
            .color(.darkGray)
            .edgesToSuperview(h: 8, v: 4)
    }
    .border(1, .darkGray)
    .circle()
    UView().width(to: v1)
}
.alignment(.center)
.spacing(10)
.edgesToSuperview(h: 0)
.centerYInSuperview()

第二种解决方案

The stack view itself can't center inner views automatically unfortunately, so you have to help it to do that.不幸的是,堆栈视图本身无法自动将内部视图居中,因此您必须帮助它做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM