gpt4 book ai didi

swift - 使用 SnapKit 到纵横比和更小的尺寸

转载 作者:行者123 更新时间:2023-12-03 18:44:46 26 4
gpt4 key购买 nike

我正在尝试在 SnapKit 中创建此布局的约束,但我很难设置最大大小 (lessThanOrEqual)。

let maxWidthContainer: CGFloat = 435
let maxHeightContainer: CGFloat = 600

containerView.snp.makeConstraints {
$0.width.equalTo(containerView.snp.height).multipliedBy(maxWidthContainer / maxHeightContainer)
$0.centerX.centerY.equalToSuperview()
$0.leading.trailing.equalToSuperview().inset(38).priorityLow()
}

最佳答案

如果我明白你要做什么,应该这样做:

    let v = UIView()
v.backgroundColor = .blue
view.addSubview(v)

let maxWidthContainer: CGFloat = 435
let maxHeightContainer: CGFloat = 600

v.snp.makeConstraints { (make) in
// centered X and Y
make.centerX.centerY.equalToSuperview()

// at least 38 points "padding" on all 4 sides

// leading and top >= 38
make.leading.top.greaterThanOrEqualTo(38)

// trailing and bottom <= 38
make.trailing.bottom.lessThanOrEqualTo(38)

// width ratio to height
make.width.equalTo(v.snp.height).multipliedBy(maxWidthContainer/maxHeightContainer)

// width and height equal to superview width and height with high priority (but not required)
// this will make it as tall and wide as possible, until it violates another constraint
make.width.height.equalToSuperview().priority(.high)

// max height
make.height.lessThanOrEqualTo(maxHeightContainer)
}

关于swift - 使用 SnapKit 到纵横比和更小的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57894335/

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