2017-11-02 13 views
0

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

私は enter image description here

は、しかし、私は子供を見ることができなかった期待する結果として生じる制約がありますビュー(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を計算していることが明らかになりました。

enter image description here

どのようにそれをすることができますか?誰が私の間違いを教えてくれるの?事前に多くの感謝!

+0

スーパービューが正しく配置され、幅と高さがゼロではありませんか? –

+0

私は単に時々あまりにも愚かです!制約を使用する代わりに、外側のNSViewのサイズを直接設定しました。初心者の間違い。 @ KamilSzostakowski:あなたの質問が私のミスを見つけました。ありがとうございました –

答えて

1

Kamil Szostakowskiが正しい質問をしました。 Autolayoutは手動サイズの設定ではうまく機能しません。制約を使ってNSViewのフレームサイズを設定する必要があります。 ありがとうKamil。

+0

私は助けてうれしいです。 –

関連する問題