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
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. I've tried every single type of distribution
but that didn't work. I've also tried to insert transparent UIView
s at the extremes to force the centering but that seems like a hack. Any ideas?