簡體   English   中英

視圖高度包裝子視圖內容

[英]View height wrap child views content

我想在超級視圖的左側添加一個圖像,並在超級視圖的自定義標簽中心添加一個圖像。 另外,superview的高度必須包裹孩子。 我也將Superview添加到堆棧中(填充,填充分布和對齊)。 代碼在下面,但未顯示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替換它,您應該會很好。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM