2016년 5월 19일 목요일

NSSplitView and NSSplitViewController Cheatsheet

NSSplitView 그리고 NSSplitViewController를 사용하면서 겪거나 필요한 내용을 정리한다. 기본적으로 NSSplitViewController 가 대상이다. 이 글은 지속적으로 업데이트 될 수도 있다.

Count of Panes

얼마나 많이 쪼게어져서 뷰컨트롤러가 삽입되어 있을까. 해답은 그냥 splitViewItems 리스트를 보면 된다.
return self.splitViewItems.count

Insertion

코드로 새 뷰 컨트롤러를 삽입하려면 아래와 같이 하면 된다.
let viewController = SomeViewController()
let item = NSSplitViewItem(viewController: viewController)
self.insertSplitViewItem(item, atIndex: 1)

Remove Pane

코드로 기존 뷰 컨트롤러를 제거하려면 아래와 같이 하면 된다.
self.removeSplitViewItem(self.splitViewItems[someIndex])

Maximum and Minimum Pane Size

쪼게진 분리 막대가 움직이는 양을 제한할 수 있을까. 안타깝지만 오토레이아웃(Auto Layout)을 쓸 때는 delegate 의 많은 내용이 제한되어서 어렵다. 대신 각 아이템(뷰컨트롤러)의 제약(Constraints)을 추가하는 형식으로 구현하는 건 가능하다. 아래는 이 기능을 함수로 분리해 놓은 것이다.
func setMinimumSize(size: CGSize, paneAtIndex: Int) {
  let view = self.splitViewItems[paneAtIndex].viewController.view
        
  let constraint = NSLayoutConstraint(item: view,
                                      attribute: .Width,
                                      relatedBy: .GreaterThanOrEqual,
                                      toItem: nil,
                                      attribute: .NotAnAttribute,
                                      multiplier: 1.0,
                                      constant: size.width)
  view.addConstraint(constraint)

  let constraint = NSLayoutConstraint(item: view,
                                      attribute: .Height,
                                      relatedBy: .GreaterThanOrEqual,
                                      toItem: nil,
                                      attribute: .NotAnAttribute,
                                      multiplier: 1.0,
                                      constant: size.height)
  view.addConstraint(constraint)
}
    
func setMaximumSize(size: CGSize, paneAtIndex: Int) {
  let view = self.splitViewItems[paneAtIndex].viewController.view
        
  let constraint = NSLayoutConstraint(item: view,
                                      attribute: .Width,
                                      relatedBy: .LessThanOrEqual,
                                      toItem: nil,
                                      attribute: .NotAnAttribute,
                                      multiplier: 1.0,
                                      constant: size.width)
  view.addConstraint(constraint)

  let constraint = NSLayoutConstraint(item: view,
                                      attribute: .Height,
                                      relatedBy: .LessThanOrEqual,
                                      toItem: nil,
                                      attribute: .NotAnAttribute,
                                      multiplier: 1.0,
                                      constant: size.height)
  view.addConstraint(constraint)
}

댓글 없음 :