【发布时间】:2020-09-15 08:20:02
【问题描述】:
我正在以这种方式在我的视图控制器中添加一个 stackView:
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.anchor(top: nameLabel.bottomAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor)
let emotionsButton = EmotionButton()
emotionsButton.translatesAutoresizingMaskIntoConstraints = false
let contentButton = ContentsButton()
contentButton.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .equalCentering
stackView.spacing = 8
stackView.addArrangedSubview(emotionsButton)
stackView.addArrangedSubview(contentButton)
emotionsButton.widthAnchor.constraint(equalToConstant: 91).isActive = true
contentButton.widthAnchor.constraint(equalToConstant: 91).isActive = true
emotionsButton.heightAnchor.constraint(equalToConstant: 83).isActive = true
contentButton.heightAnchor.constraint(equalToConstant: 83).isActive = true
但我得到的结果不是我想要的;这就是我想要的:
这是我得到的:
【问题讨论】:
-
使用 stackview 可能有效,但我建议您可以创建一个 UIVIew,并将您的两个视图放入其中。然后在“UIView”(父视图)上水平居中
标签: swift uibutton uistackview