4:3でなければならない子UIViewを含むUIViewがあるとしましょう。コードでAutoLayoutを使用してこの問題を解決したいと思います。
私はAutoLayoutに苦労してきましたが、これを行う方法をまだ理解していません。
この問題を解決する方法を知っていますか?
どうもありがとうございます。
意味をわかりやすく説明するために画像を添付します。 http://d.pr/i/d0Oc
4:3でなければならない子UIViewを含むUIViewがあるとしましょう。コードでAutoLayoutを使用してこの問題を解決したいと思います。
私はAutoLayoutに苦労してきましたが、これを行う方法をまだ理解していません。
この問題を解決する方法を知っていますか?
どうもありがとうございます。
意味をわかりやすく説明するために画像を添付します。 http://d.pr/i/d0Oc
私は理解しました。
//Given a childView...
NSLayoutConstraint *constraint =[NSLayoutConstraint
constraintWithItem:childView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:childView
attribute:NSLayoutAttributeHeight
multiplier:4.0/3.0 //Aspect ratio: 4*height = 3*width
constant:0.0f];
[childView addConstraint:constraint];
Swift3の構文は次のとおりです。
NSLayoutConstraint(
item: item,
attribute: .width,
relatedBy: .equal,
toItem: item,
attribute: .height,
multiplier: width / height,
constant: 0)
私は上記のすべての答えを試しましたが、うまくいきませんでした、そしてこれは迅速な3での私の解決策です:
let newConstraint = NSLayoutConstraint(item: yourView, attribute: .width, relatedBy: .equal, toItem: yourView, attribute: .height, multiplier: 560.0/315.0, constant: 0)
yourView.addConstraint(newConstraint)
NSLayoutConstraint.activate([newConstraint])
NSLayoutConstraint.deactivate(yourView.constraints)
yourView.layoutIfNeeded()