如何解决在stackview程序mcatilly中的uibutton下获取imageview
我的快速代码目标是在下面直接放置一个uibutton。您可以看到左侧的第一张图片是下面的代码在做什么。您可以看到第一个图像将imageview放在按钮的末尾。目的是使代码看起来像第二个图像,其中黄色的imageview位于按钮的正下方。第一个按钮下只能有一个imageview。
import UIKit
class ViewController: UIViewController {
let Box = (0..<20).map { _ in UIButton() }
let stackView = UIStackView()
var image1 = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
image1.backgroundColor = .orange
[stackView].forEach{
$0.axis = .horizontal
$0.alignment = .fill
$0.distribution = .equalSpacing
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
Box[1...6].forEach { UIButton in
stackView.addArrangedSubview(UIButton)
//added
stackView.addArrangedSubview(image1)
//
UIButton.backgroundColor = .red
// make the image views square (1:1 ratio ... height == width)
UIButton.heightAnchor.constraint(equalTo: UIButton.widthAnchor).isActive = true
image1.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.02).isActive = true
// image1.topAnchor.constraint(equalTo: UIButton.bottomAnchor).isActive = true
// each image view is 10% of the width of the view
UIButton.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 0.12).isActive = true
image1.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 0.02).isActive = true
image1.topAnchor.constraint(equalTo: UIButton.bottomAnchor).isActive = true
}
stackView.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1).isActive = true
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 20.0).isActive = true
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 0.0).isActive = true
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。