gpt4 book ai didi

swift - 使用自动布局将 3 个 ImageView 实际放置在相同的高度

转载 作者:行者123 更新时间:2023-11-30 10:31:16 25 4
gpt4 key购买 nike

下面的代码声明了 3 个 ImageView 。我希望它们都具有相同的高度并填充 uiview。它应该看起来像下面这张照片

enter image description here

我需要以某种方式让 ImageView 相互交互,但我不知道如何。这应该在所有代码中完成。没有使用 Storyboard。

    import UIKit

class ViewController: UIViewController {

var pic = UIImageView()
var pic2 = UIImageView()
var pic3 = UIImageView()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[pic,btn].forEach({
$0.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview($0)
$0.backgroundColor = .systemRed
})
pic3.backgroundColor = .systemOrange
pic2.backgroundColor = .systemPink


pic.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
pic.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15).isActive = true
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15).isActive = true

pic2.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
pic2.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100).isActive = true
pic2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15).isActive = true
pic2.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15).isActive = true

pic3.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
pic3.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100).isActive = true
pic3.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15).isActive = true
pic3.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15).isActive = true
}
}

最佳答案

使用自动布局约束:对齐边缘,然后使用 View (或 SafeAreaLayoutGuide 的)高度 anchor 和三分之一的乘数。为了允许舍入,仅将高度 anchor 分配给三个 View 中的两个,并使用顶部/底部 anchor 设置剩余 View 的高度。

   let view1 = UIView()
view1.translatesAutoresizingMaskIntoConstraints = false
view1.backgroundColor = .blue
let view2 = UIView()
view2.translatesAutoresizingMaskIntoConstraints = false
view2.backgroundColor = .yellow
let view3 = UIView()
view3.translatesAutoresizingMaskIntoConstraints = false
view3.backgroundColor = .purple
view.addSubview(view1)
view.addSubview(view2)
view.addSubview(view3)

//set constraints
NSLayoutConstraint.activate([
// anchor edges
view1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
view1.trailingAnchor.constraint(equalTo: view.trailingAnchor),
view2.leadingAnchor.constraint(equalTo: view.leadingAnchor),
view2.trailingAnchor.constraint(equalTo: view.trailingAnchor),
view3.leadingAnchor.constraint(equalTo: view.leadingAnchor),
view3.trailingAnchor.constraint(equalTo: view.trailingAnchor),
// set anchors governing height
view1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
view2.topAnchor.constraint(equalTo: view1.bottomAnchor),
view2.bottomAnchor.constraint(equalTo: view3.topAnchor),
view3.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
view1.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/3),
view2.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/3)
])

关于swift - 使用自动布局将 3 个 ImageView 实际放置在相同的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59167478/

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