ios - UIImageView 忽略宽度/高度约束

标签 ios swift uiimageview autolayout

这是我的课:

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)
    }
}

这是结果:

enter image description here

(橙色是我的 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

关于ios - UIImageView 忽略宽度/高度约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46248429/

相关文章:

ios - Swift 中是否有默认的加密功能来加密/解密字符串?

swift - 如何在特定时间后重新加载 TableView

swift - UIImage 未出现在 UITableView 的原型(prototype)单元格中(Swift)

ios - 为什么后台函数没有被调用?

iphone - UITableViewController 中的 tableView 在初始化后为 nil

ios - 观看视频广告后在屏幕上更新分数

xcode - 如何本地化同名图像?

ios - 滚动 collectionview 时数据已被替换或丢失

ios - 语音识别和音频播放可以在模拟器上运行,但不能在 iPhone 上运行?

ios - 如何在 UIImageView 底部应用曲线?