2016-08-27 8 views
0

この画像を画像ビューに配置し、アスペクトに合わせるだけです。実行時に自動レイアウトで画像ビュー枠が正しく設定されていない

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

+0

を設定した後ImageViewの中に画像をロードしようとしますこれは、回転などの状況で画像ビューが拡大縮小されません。これを解決するために私の答えを参照してください。 –

関連する問題