2014-09-06 2 views
2

NSStackView 내 뷰를 차례로 배치하려면 어떻게해야합니까? 파란색 상자가 빨간색 상자 위에 그려져 있습니다.스위프트 NSStackView : 배우기. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

import Cocoa 

class TestView : NSView{ 
    override init() { 
    super.init(frame: NSRect(x: 0,y: 0,width: 100,height: 100)) 
    } 

    required init(coder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func drawRect(dirtyRect: NSRect) { 
    super.drawRect(dirtyRect) 
    NSColor.redColor().setFill() 
    NSBezierPath.fillRect(self.bounds) 
    } 
} 

class TestView2 : NSView{ 
    override init() { 
    super.init(frame: NSRect(x: 0,y: 0,width: 100,height: 100)) 
    } 

    required init(coder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 

    override func drawRect(dirtyRect: NSRect) { 
    super.drawRect(dirtyRect) 
    NSColor.blueColor().setFill() 
    NSBezierPath.fillRect(self.bounds) 
    } 
} 

class AppDelegate: NSObject, NSApplicationDelegate { 

    @IBOutlet weak var window: NSWindow! 

    @IBOutlet weak var searchFacetItems: SearchFacetSelectorView! 
    @IBOutlet weak var searchFacetHeader: SearchFacetSelectorHeader! 
    var content : NSStackView! 
    let testView = TestView() 
    let testView2 = TestView2() 

    func applicationDidFinishLaunching(aNotification: NSNotification?) { 
    // Insert code here to initialize your application 
    content = NSStackView(frame: window.contentView.bounds) 
     content.orientation = NSUserInterfaceLayoutOrientation.Vertical 
     content.alignment = NSLayoutAttribute.CenterX 

    content.addView(testView, inGravity: NSStackViewGravity.Center) 
    content.addView(testView2, inGravity: NSStackViewGravity.Center) 


    window.contentView = content! 
    } 

    func applicationWillTerminate(aNotification: NSNotification?) { 
    // Insert code here to tear down your application 
    } 


} 

그것은 AppKit의 아니에요을 읽어 배우,하지만 어쩌면 내가 몇 가지 단서를 얻을 수 있습니다 : HOW TO USE UIVIEWS WITH AUTO LAYOUT PROGRAMMATICALLY

Auto Layout Guide

답변

0

당신은 NSRect를 조정하여이 작업을 수행 할 수 있어야한다 좌표. 수직으로 아래

NSRect(0, 0, 100, 100) // TestView1 
NSRect(100, 0, 100, 100) // TestView2 

:

당신이 다른 옆에있는 (오른쪽) 수평 NSView를 원하는 경우

NSRect(x: 0, y: 0, width: 100, height: 100) 

: 현재 동일한 정확한 좌표를 사용하여 두 NSRects있어

NSRect(0, 0, 100, 100) // TestView1 
NSRect(0, -100, 100, 100) // TestView2 
+0

이전에 시도했지만 작동합니다. 그러나 나는 스택 뷰가 자신이 현명하게 관리하는 뷰를 레이아웃 할 것을 기대하고있다. –

0

보기에는 intrinsicContentSize 또는 명시 적 제한과 같이 원하는 크기를 설명하는 제약 조건이 없습니다. NSStackView은 개별보기의 크기를 정하지 않고 누적보기를 서로 상대적으로 배치하는 제약 조건 만 추가합니다.

스태킹 축에서 그 크기가 모호 해져 일반적으로 모든 크기 조정을 단일보기로 제공합니다. 귀하의 예제에서, 나는 파란색 상자가 빨간색 상자 위에 그려지지 않고 빨간색 상자의 높이가 0이고 파란색보기 뒤에 쌓여 있다고 생각합니다.

스택보기에 NSButton을 추가 할 때 정의 된 intrinsicContentSize이 있으므로이 문제가 없습니다. 귀하의 의견이 무엇인지에 따라

은 - 그들이 고유 크기가 수행하거나 내부 파단에 대한 제약 조건에 의해 정의 될 것이다 - 당신은 intrinsicContentSize을 무시하거나 자신의 높이를 정의 결국 그 제약 조건을 추가 할 것 중 하나 .


편집 : 아, 그리고 translatesAutoresizingMaskIntoConstraints는 당신이 스택보기에 추가하는 견해 (샘플 코드에서처럼은 보이지 않는)에 false로 설정되어 있는지 확인합니다. 그렇지 않으면 자동 크기 조정 제약 조건이 뷰를 배치하고 스택 뷰가 추가하는 제약 조건과 충돌하는 위치에서 문제가 발생합니다.