[英]Verticial UIStackView center align two UILabels inside
There is a Root UIStackView with an ImageView and Information UIStackView with two UILabels.有一个带有 ImageView 的 Root UIStackView 和带有两个 UILabels 的 Information UIStackView。 I'm attempting to center the two labels in the UIStackView.我试图在 UIStackView 中将两个标签居中。 I have tried adding a center constraint to the stack view to the center of the image.我试过在图像中心的堆栈视图中添加一个中心约束。 The top label grows and doesn't hug it's content.顶部标签增长并且不拥抱它的内容。
private let stack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .leading
stackView.spacing = 8
return stackView
}()
private let informationStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.alignment = .leading
return stackView
}()
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
let stack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.spacing = 8
return stackView
}()
let informationStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.alignment = .leading
return stackView
}()
let timeLabel = UILabel()
timeLabel.text = "9:30 AM"
timeLabel.textColor = .white
informationStack.addArrangedSubview(timeLabel)
let eventLabel = UILabel()
eventLabel.textColor = .white
eventLabel.font = eventLabel.font.withSize(24)
eventLabel.text = "Faith, Hope & Love"
informationStack.addArrangedSubview(eventLabel)
let drizzle = UIImage(systemName: "cloud.drizzle.fill")
let drizzleView = UIImageView(image: drizzle)
drizzleView.translatesAutoresizingMaskIntoConstraints
drizzleView.contentMode = .scaleAspectFit
drizzleView.backgroundColor = .white
drizzleView.layer.cornerRadius = 16
drizzleView.widthAnchor.constraint(equalToConstant: 175).isActive = true
drizzleView.heightAnchor.constraint(equalToConstant: 100).isActive = true
stack.addArrangedSubview(drizzleView)
stack.addArrangedSubview(informationStack)
debugPrint(stack.arrangedSubviews)
stack.bounds = CGRect(x: 0,y: 0,width: 512,height: 125)
stack.backgroundColor = .black
PlaygroundSupport.PlaygroundPage.current.liveView = stack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.