Androidでは次のように指定することで、高さが自動で確定することができました。
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
が、iOSでは簡単にはできず、カスタムViewを作ることで解決しました。
UIImageViewのimageがセットされる際に、アスペクト比を計算してConstraintを確定しているだけです。
class AspectAdjuestUIImageView: UIImageView {
internal var aspectConstraint : NSLayoutConstraint? {
didSet {
if oldValue != nil {
self.removeConstraint(oldValue!)
}
if aspectConstraint != nil {
self.addConstraint(aspectConstraint!)
}
}
}
override var image: UIImage?{
willSet{
let aspect = newValue!.size.width / newValue!.size.height
aspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: aspect, constant: 0.0)
}
}
}

0 件のコメント:
コメントを投稿