2017-03-05 10 views
2

2つのアイテムがスタックビューにあり、1つはイメージで、もう1つはラベルです。私はフレームサイズに応じて画像のサイズを変更したいのですが、スタックのサイズではなく、これをどのように変更することができますか?Swiftスタックビュー内のイメージのサイズを変更する方法

//stack code 
lazy var releaseInfoStack:UIStackView = { 
     let v = UIStackView() 
     v.translatesAutoresizingMaskIntoConstraints = false 
     v.spacing = 4 
     v.alignment = .center 
     v.axis = .horizontal 
     return v 
    }() 

//my image 
lazy var smallRealeaseImageIcon:UIImageView = { 
     let v = UIImageView(frame:CGRect(x: 0, y: 0, width: 17, height: 17)) 
     v.contentMode = .scaleAspectFit 
     v.image = UIImage(named: "link") 
     return v 
    }() 

//this is the current image below but i want the link icon to be smaller 

Current Image

答えて

2

あなたは高さと幅の制​​約を追加することができます。

let imageViewWidthConstraint = NSLayoutConstraint(item: smallRealeaseImageIcon, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 20) 
let imageViewHeightConstraint = NSLayoutConstraint(item: smallRealeaseImageIcon, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 20) 
smallRealeaseImageIcon.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint]) 

ただ、比例にstackviewの分布を設定してください:

releaseInfoStack.distribution = .fillProportionally 
関連する問題