gpt4 book ai didi

ios - 在 iOS 堆栈 View 中水平居中 View

转载 作者:行者123 更新时间:2023-12-01 21:58:14 31 4
gpt4 key购买 nike

我正在尝试实现以下布局( View 水平居中):

enter image description here

所以我设置了一个堆栈 View ,如下所示:

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

向堆栈 View 添加按钮:
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)
}
}
}

按钮类:
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")
}
}

这就是我添加堆栈 View 的方式,我将它添加到 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)
}

但是,这就是我得到的:

enter image description here

堆栈 View 具有 100% 的屏幕宽度。我已经尝试了每一种类型的 distribution但这没有用。我也试过插入透明 UIView s 在极端情况下强制居中,但这似乎是一种 hack。有任何想法吗?

最佳答案

只需在其父 View 中水平居中您的堆栈 View 即可。

以下示例使用 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()

First solution

或者通过添加两个宽度相等的 View 来使用一点技巧,一个在堆栈的开头,一个作为堆栈中的最后一个 View 。

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()

Second solution

不幸的是,堆栈 View 本身不能自动居中内部 View ,因此您必须帮助它做到这一点。

关于ios - 在 iOS 堆栈 View 中水平居中 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60953602/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com