【发布时间】:2018-11-21 11:10:06
【问题描述】:
我以编程方式制作的 StackView 如下所示:
两个子视图:
三个子视图:
四个子视图:
这是我的代码:
cameraButton.setImage(UIImage(named: "blueCamera"), for: .normal)
cameraButton.addTarget(self, action: #selector(cameraTapped), for: .touchUpInside)
cameraButton.widthAnchor.constraint(equalToConstant: 70.0).isActive = true
calenderButton.setImage(UIImage(named: "calender"), for: .normal)
calenderButton.addTarget(self, action: #selector(calanderTapped), for: .touchUpInside)
calenderButton.widthAnchor.constraint(equalToConstant: 70.0).isActive = true
stackView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollViewHeight)
stackView.axis = .horizontal
stackView.spacing = 5
stackView.semanticContentAttribute = .forceLeftToRight
stackView.alignment = .fill // .leading .firstBaseline .center .trailing .lastBaseline
stackView.distribution = .equalSpacing // .fillEqually .fillProportionally .equalSpacing .equalCentering
stackView.addArrangedSubview(calenderButton)
stackView.addArrangedSubview(cameraButton)
stackView.backgroundColor = UIColor(red: 0.161, green: 0.165, blue: 0.188, alpha: 1.00)
scrollView.addSubview(stackView)
有什么问题: StackView 没有将子视图向左对齐。
我想要达到的目标:
我希望所有的子视图都向左对齐,我尝试使用stackView.semanticContentAttribute = .forceLeftToRight 来做到这一点,但这似乎并没有多大效果。
【问题讨论】:
-
试试 stackView.alignment = .leading
-
@Alexander Nope.
-
带走semanticContentAttribute?
-
您需要将 4 个视图放入具有清晰颜色的堆栈视图中,然后在这些视图中添加按钮
标签: ios swift uistackview stackview