【问题标题】:Image grows even if content/compression is set to 1即使内容/压缩设置为 1,图像也会增长
【发布时间】:2017-09-15 11:58:48
【问题描述】:

这是我想以编程方式创建的内容:

那就是一个UIView,里面有一个UIImageView和一个UILabel。一些注意事项:

  • 图像应始终为 1:1
  • 图片高度应与标签高度一致
  • 如果标签中的文本变宽,视图也应该变宽。
  • 视图的高度应该适应标签的高度,所以我没有设置 UIView 的任何高度限制。
  • 请参阅此 github 项目以获取我想要的示例:https://github.com/Jasperav/constrains

这是我的代码,你可以复制粘贴它,但请确保为 UIImageView 的图像设置其他字符串:

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

    }

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

func load(){
    let overlappingView = UIView()
    overlappingView.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(overlappingView)

    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "0"
    label.textColor = .white
    label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 751), for: .horizontal)
    label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 999), for: .vertical)
    label.setContentHuggingPriority(UILayoutPriority(rawValue: 999), for: .vertical)
    label.setContentHuggingPriority(UILayoutPriority(rawValue: 999), for: .horizontal)

    let image = UIImageView()
    image.image = UIImage(named: "Test")
    image.translatesAutoresizingMaskIntoConstraints = false
    image.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: .horizontal)
    image.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: .vertical)
    image.setContentHuggingPriority(UILayoutPriority(rawValue: 1), for: .vertical)
    image.setContentHuggingPriority(UILayoutPriority(rawValue: 1), for: .horizontal)

    overlappingView.addSubview(label)
    overlappingView.addSubview(image)

    overlappingView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    overlappingView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -2).isActive = true
    overlappingView.widthAnchor.constraint(greaterThanOrEqualTo: self.widthAnchor, multiplier: 0.6).isActive = true

    label.topAnchor.constraint(equalTo: overlappingView.topAnchor, constant: 5).isActive = true
    label.bottomAnchor.constraint(equalTo: overlappingView.bottomAnchor, constant: -5).isActive = true
    label.trailingAnchor.constraint(equalTo: overlappingView.trailingAnchor, constant: -5).isActive = true
    label.heightAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true

    image.centerYAnchor.constraint(equalTo: overlappingView.centerYAnchor).isActive = true
    image.leadingAnchor.constraint(equalTo: overlappingView.leadingAnchor, constant: 2).isActive = true
    image.widthAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
    image.trailingAnchor.constraint(equalTo: label.trailingAnchor, constant: -10).isActive = true

    }
}

使用此代码,我看到图像以 1:1 的大小占据了整个屏幕的高度。为什么它不尊重标签高度?我在界面生成器中有相同的约束,为什么它在代码中不起作用?

谢谢。

【问题讨论】:

    标签: ios swift autolayout


    【解决方案1】:

    您好,我对您的代码进行了一些修改,并对其进行了修复。我添加了一些颜色,以便您轻松看到差异。如果您有更多问题,我会再次帮助您。

    class View2: UIView{
        override init(frame: CGRect) {
            super.init(frame: frame)
            load()
    
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            load()
        }
    
        func load(){
            self.backgroundColor = .green
            let overlappingView = UIView()
            overlappingView.backgroundColor = .red
            overlappingView.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(overlappingView)
    
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.text = "0"
            label.textColor = .black
    
    
            let image = UIImageView(frame:CGRect(x: 10, y: 10, width: 10, height: 10))
            image.image = UIImage(named: "btn_back")
            image.tintColor = .black
            image.backgroundColor = .blue
            image.translatesAutoresizingMaskIntoConstraints = false
            image.setContentCompressionResistancePriority(1, for: .horizontal)
    
    
            overlappingView.addSubview(label)
            overlappingView.addSubview(image)
    
            label.sizeToFit()
    
            overlappingView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
            overlappingView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -2).isActive = true
            overlappingView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1.0).isActive = true
            overlappingView.heightAnchor.constraint(equalToConstant: label.frame.height + 10).isActive = true
    
            label.topAnchor.constraint(equalTo: overlappingView.topAnchor, constant: 5).isActive = true
            label.bottomAnchor.constraint(equalTo: overlappingView.bottomAnchor, constant: -5).isActive = true
            label.trailingAnchor.constraint(equalTo: overlappingView.trailingAnchor, constant: -5).isActive = true
            label.heightAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
    
    
            image.centerYAnchor.constraint(equalTo: overlappingView.centerYAnchor).isActive = true
            image.leadingAnchor.constraint(equalTo: overlappingView.leadingAnchor, constant: 2).isActive = true
            image.widthAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
            image.trailingAnchor.constraint(equalTo: label.leadingAnchor, constant: -10).isActive = true
    
    
        }
    }
    

    【讨论】:

    • 您好,感谢您的帮助!我没有设置重叠视图的高度,因为它应该适应标签的高度。在 Interface Builder 中,我这样做没有问题。是否可以不为重叠视图设置显式高度以适应标签的文本高度?
    • 检查我更新的决定。现在我得到标签高度并为您在下面设置的填充添加 10 像素
    • 我不明白。在界面生成器中,我没有设置它并且它可以工作。我将我的项目上传到 github,你可以看到我使用了与我的问题完全相同的约束,但它不会在代码中工作。
    • 是的,您的代码可以正常工作,但为什么我需要调用 label.sizeToFit() 才能使其正常工作?如果必须调用该函数才能使其工作,那么在我看来,这些约束有问题。
    • 关于您的代码的另一件事:equalToConstant:label.frame.height + 10 返回 0 + 10,因为 label.frame.height 在加载函数中始终为 0,无论标签中的输入如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2022-12-03
    • 1970-01-01
    • 2015-01-28
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多