gpt4 book ai didi

ios - View 高度换行 subview 内容

转载 作者:行者123 更新时间:2023-11-30 11:02:33 26 4
gpt4 key购买 nike

我想添加 super View 左侧的图像和 super View 的自定义标签中心。另外 super View 的高度必须包裹 subview 。另外,我将 super View 添加到堆栈中(填充、填充分布和对齐)。代码如下,但 ImageView 未显示。这里有什么问题?

let topView = UIView()
topView.translatesAutoresizingMaskIntoConstraints = false
topView.heightAnchor.constraint(equalToConstant: 30).isActive = true
topView.widthAnchor.constraint(equalToConstant: self.view.frame.size.width).isActive = true

let backImageView: UIImageView = UIImageView()
backImageView.isUserInteractionEnabled = true
backImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backButtonClicked)))
backImageView.image = UIImage(named: "backButton")
backImageView.contentMode = .scaleAspectFit
backImageView.clipsToBounds = true

topView.addSubview(backImageView)
backImageView.leftAnchor.constraint(equalTo: topView.leftAnchor).isActive = true
backImageView.topAnchor.constraint(equalTo: topView.topAnchor).isActive = true
backImageView.bottomAnchor.constraint(equalTo: topView.bottomAnchor).isActive = true
backImageView.centerYAnchor.constraint(equalTo: topView.centerYAnchor).isActive = true

topView.addSubview(titleText)
titleText.centerXAnchor.constraint(equalTo: topView.centerXAnchor).isActive = true
titleText.centerYAnchor.constraint(equalTo: topView.centerYAnchor).isActive = true

最佳答案

我认为你的问题出在 backImageView Y 约束

backImageView.centerYAnchor.constraint(equalTo: topView.centerYAnchor).isActive = true

应该是

backImageView.centerXAnchor.constraint(equalTo: topView.centerXAnchor).isActive = true

Y 代表垂直轴,因此在您的 4 个约束中,您有 3 个垂直约束和 1 个水平约束。将其替换为 centerXAnchor 就可以了。

enter image description here

关于ios - View 高度换行 subview 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53157158/

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