横向 UIStackView 拉伸子视图
Horizontal UIStackView stretching subview
我有以下游乐场,无法弄清楚为什么 ImageView 宽度约束没有得到遵守,它填充了 StackView
上剩余的 space
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 800))
let imageView = UIView(frame: CGRect.zero)
imageView.backgroundColor = UIColor.red
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
let navBarHeaderStackView = UIStackView()
navBarHeaderStackView.distribution = .equalCentering
navBarHeaderStackView.alignment = .center
navBarHeaderStackView.axis = .horizontal
navBarHeaderStackView.addArrangedSubview(imageView)
//navBarHeaderStackView.addArrangedSubview(titlesStackView)
view.addSubview(navBarHeaderStackView)
navBarHeaderStackView.translatesAutoresizingMaskIntoConstraints = false
navBarHeaderStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50).isActive = true
navBarHeaderStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 8).isActive = true
navBarHeaderStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
navBarHeaderStackView.heightAnchor.constraint(equalToConstant: 60).isActive = true
navBarHeaderStackView.backgroundColor = UIColor.blue
PlaygroundPage.current.liveView = view
因为这个限制
navBarHeaderStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 8).isActive = true
发表评论并尝试
将 trailingAnchor
设置为 lessThanOrEqualTo
而不是 equalTo
。
navBarHeaderStackView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: 8).isActive = true
我有以下游乐场,无法弄清楚为什么 ImageView 宽度约束没有得到遵守,它填充了 StackView
上剩余的 spaceimport UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 800))
let imageView = UIView(frame: CGRect.zero)
imageView.backgroundColor = UIColor.red
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
let navBarHeaderStackView = UIStackView()
navBarHeaderStackView.distribution = .equalCentering
navBarHeaderStackView.alignment = .center
navBarHeaderStackView.axis = .horizontal
navBarHeaderStackView.addArrangedSubview(imageView)
//navBarHeaderStackView.addArrangedSubview(titlesStackView)
view.addSubview(navBarHeaderStackView)
navBarHeaderStackView.translatesAutoresizingMaskIntoConstraints = false
navBarHeaderStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50).isActive = true
navBarHeaderStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 8).isActive = true
navBarHeaderStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
navBarHeaderStackView.heightAnchor.constraint(equalToConstant: 60).isActive = true
navBarHeaderStackView.backgroundColor = UIColor.blue
PlaygroundPage.current.liveView = view
因为这个限制
navBarHeaderStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 8).isActive = true
发表评论并尝试
将 trailingAnchor
设置为 lessThanOrEqualTo
而不是 equalTo
。
navBarHeaderStackView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: 8).isActive = true