即使内容/压缩设置为1,图像也会增长

时间:2017-09-15 11:58:48

标签: ios swift autolayout

这是我想以编程方式创建的: enter image description here

这是一个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的大小占据整个屏幕的高度。为什么它不尊重标签高度?我在界面构建器中有相同的约束,为什么它在代码中不起作用?

谢谢。

1 个答案:

答案 0 :(得分:0)

您好我用您的代码玩了一下我修复它。我添加一些颜色,这样您就可以轻松看到差异。如果您有更多问题,我会再次帮助您。

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


    }
}