图像视图框架未通过自动布局正确设置

时间:2016-08-27 11:35:53

标签: ios swift autolayout

只需将此图像放在图像视图中并设置为宽高比即可。

enter image description here

override func viewDidLoad() {
        super.viewDidLoad()
        let image = UIImage(named: "1-landscape")
        let imageView = UIImageView(image: image)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.blackColor()
        imageView.contentMode = .ScaleAspectFit
        view.addSubview(imageView)
        var collector = [NSLayoutConstraint]()
        collector.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[imageView]-0-|", options: [], metrics: nil, views: ["imageView": imageView]))
        collector.append(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
        view.addConstraints(collector)
    }

在运行时,视图的高度比图像的高度大50%。为什么???

enter image description here

如果我们用旧学校的方式发表意见,就没有问题。我忘记了自动布局的步骤吗?

    override func viewDidLoad() {
        super.viewDidLoad()
        let image = UIImage(named: "1-landscape.png")!
        let scaleFactor = image.size.height / image.size.width
        let width = view.frame.size.width
        let height = width * scaleFactor
        let size = CGSize(width: width, height: height)
        let imageView = UIImageView(image: image)
//        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.blackColor()
        imageView.contentMode = .ScaleAspectFit
        imageView.frame.size = size
        imageView.center = view.center
        view.addSubview(imageView)
//        var collector = [NSLayoutConstraint]()
//        collector.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[imageView]-0-|", options: [], metrics: nil, views: ["imageView": imageView]))
//        collector.append(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
//        view.addConstraints(collector)
    }

2 个答案:

答案 0 :(得分:0)

使用锁定宽高比的约束强制高度,就可以了。

override func viewDidLoad() {
        super.viewDidLoad()
        let image = UIImage(named: "1-landscape.png")!
        let imageView = UIImageView(image: image)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.blackColor()
        imageView.contentMode = .ScaleAspectFit
        view.addSubview(imageView)
        var collector = [NSLayoutConstraint]()
        collector.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[imageView]-0-|", options: [], metrics: nil, views: ["imageView": imageView]))
        collector.append(NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: imageView, attribute: .Width, multiplier: image.size.height / image.size.width, constant: 0))
        collector.append(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
        view.addConstraints(collector)
    }

答案 1 :(得分:0)

缩放图像时,仅更改图像的大小。 imageView的大小不会改变。你需要通过找到比率来改变它。

添加此

    view.layoutIfNeeded()

    let widthRatio = imageView.bounds.size.width / image!.size.width
    let heightRatio = imageView.bounds.size.height / image!.size.height
    let scale =  widthRatio < heightRatio ? widthRatio : heightRatio
    let imageHeight = scale * imageView.image!.size.height

    view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: imageHeight))

此行view.addConstraints(collector)

之后