autolayoutに関するNSViewの動作が奇妙です。Autolayoutが機能しません - 子ビューサイズは常にSuperviewと同じサイズでなければなりません
私は、このコードによって、スーパーの大きさに私の唯一の子ビューの境界をしています:
func fillHorizontal() {
guard let sv = self.superview else {
assert(false)
return
}
self.translatesAutoresizingMaskIntoConstraints = false
sv.addConstraint(NSLayoutConstraint(
item: self, attribute: .width,
relatedBy: .equal, toItem: sv,
attribute: .width, multiplier: 1, constant: 0))
sv.addConstraint(NSLayoutConstraint(
item: self, attribute: .left,
relatedBy: .equal, toItem: sv,
attribute: .left, multiplier: 1, constant: 0))
}
func fillVertical() {
guard let sv = self.superview else {
assert(false)
return
}
self.translatesAutoresizingMaskIntoConstraints = false
sv.addConstraint(NSLayoutConstraint(
item: self, attribute: .height,
relatedBy: .equal, toItem: sv,
attribute: .height, multiplier: 1, constant: 0))
sv.addConstraint(NSLayoutConstraint(
item: self, attribute: .top,
relatedBy: .equal, toItem: sv,
attribute: .top, multiplier: 1, constant: 0))
}
func bindFrameToSuperviewBounds() {
fillHorizontal()
fillVertical()
}
は、しかし、私は子供を見ることができなかった期待する結果として生じる制約がありますビュー(GraphView)。このような子ビューにsetFrameSize機能を微調整/デバッグを開始する場合:
override func setFrameSize(_ newSize: NSSize) {
if let superSize = self.superview?.frame.size, newSize == superSize { //this is quite dodgy. Not sure why I get too many zero-sized initiations.
super.setFrameSize(newSize)
}
}
それは、すべてのレイアウトエンジンは、常に、子ビューのサイズ0を計算していることが明らかになりました。
どのようにそれをすることができますか?誰が私の間違いを教えてくれるの?事前に多くの感謝!
スーパービューが正しく配置され、幅と高さがゼロではありませんか? –
私は単に時々あまりにも愚かです!制約を使用する代わりに、外側のNSViewのサイズを直接設定しました。初心者の間違い。 @ KamilSzostakowski:あなたの質問が私のミスを見つけました。ありがとうございました –