簡體   English   中英

UIImageView 忽略寬度/高度約束

[英]UIImageView ignores the width/height constraints

這是我的課:

class Some: UIView{
override init(frame: CGRect) {
    super.init(frame: frame)
    load()

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    load()
}

private func load(){
    let someImage = UIImageView()
    someImage.translatesAutoresizingMaskIntoConstraints = false
    someImage.image = UIImage(named: "Test")
    self.addSubview(someImage)
    someImage.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    someImage.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)
    someImage.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5)
    }
}

這是結果:

在此處輸入圖片說明

(橙色是我的 ViewController 的背景)

為什么我的圖像忽略了這一行:

someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)
someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)

它在圖像中顯然有 1.0 的乘數。 它實際上應該占據屏幕的一半,在高度和寬度上,如乘數 (0.5) 中所見。 我做錯了什么?

是只有我還是最后兩個約束中缺少“.isActive = true”。 例如:

someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true

暫無
暫無

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

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