NSSplitView — 分屏视图:在水平/垂直方向的线性堆栈中排列 两个或多个视图!
分屏视图也可以管理 分屏视图控制器(NSSplitViewController)的分屏和方向。默认情况下,分隔条有一个水平方向,这样分屏视图就可以从上到下垂直排列它的窗格 —— 如果vertical属性为NO(默认值),则顶部分隔条的索引为0。如果vertical为YES,则前导分隔条的索引为0。
必须使用NSSplitView实例,来放置NSSplitViewController的(view
属性或splitView
属性)视图:
//必须使用NSSplitView实例,来放置NSSplitViewController的视图
let sigV = NSSplitView(frame: NSMakeRect(10, 10, 200, 200))
self.view .addSubview(sigV)
sigV.wantsLayer = true
sigV.layer?.backgroundColor = NSColor.purple.cgColor
let splitVC = NSSplitViewController()
splitVC.view.wantsLayer = true
splitVC.view.layer?.backgroundColor = NSColor.cyan.cgColor
self .addChild(splitVC)
sigV .addSubview(splitVC.view)//✅
//sigV .addSubview(splitVC.splitView)//✅
let subVC1 = GYHViewController()
subVC1.view.wantsLayer = true
subVC1.view.layer?.backgroundColor = NSColor.red.cgColor
let subVC2 = GYHViewController()
subVC2.view.wantsLayer = true
subVC2.view.layer?.backgroundColor = NSColor.blue.cgColor
//添加分屏视图的子项
let splitV_Item1 = NSSplitViewItem(viewController: subVC1)
let splitV_Item2 = NSSplitViewItem(viewController: subVC2)
splitVC .addSplitViewItem(splitV_Item1)
splitVC .addSplitViewItem(splitV_Item2)
效果:子视图可以设置其分屏大小!
运行效果 UI界面 UI的层次结构直接使用NSSplitView:(不通过分屏视图控制器(NSSplitViewController))
添加子视图,会(默认)依次填满NSSplitView实例 — 按子视图的比例对应尺寸!
let totalV = NSSplitView(frame: NSMakeRect(20, 20, 100, 300))
self.view .addSubview(totalV)
//添加了子视图,会(默认)依次填满NSSplitView实例——按子视图比例的对应尺寸
let leftV = NSView(frame: NSMakeRect(10, 10, 100, 100))
totalV .addSubview(leftV)
leftV.wantsLayer = true
leftV.layer?.backgroundColor = NSColor.red.cgColor
let midV = NSView(frame: NSMakeRect(10, 10, 150, 150))
totalV .addSubview(midV)
midV.wantsLayer = true
midV.layer?.backgroundColor = NSColor.yellow.cgColor
let rightV = NSView(frame: NSMakeRect(10, 10, 200, 200))
totalV .addSubview(rightV)
rightV.wantsLayer = true
rightV.layer?.backgroundColor = NSColor.cyan.cgColor
//totalV .arrangesAllSubviews = false//是否分栏化——默认为YES
totalV.wantsLayer = true
//设置边框及边框颜色
totalV.layer?.borderWidth = 3.0
totalV.layer?.borderColor = NSColor.black.cgColor
效果:
UI界面 UI的层次结构设置NSSplitView的(backgroundColor)颜色后,会把‘分割条’颜色(分割条上的点 仍在显示)覆盖隐藏掉~
totalV.layer?.backgroundColor = NSColor.blue.cgColor
效果:
UI界面为NSSplitView设置分割条是否垂直 — isVertical
属性(默认为NO)
totalV.isVertical = true//分割条是否垂直(默认为NO)
效果:
UI界面也可以给NSSplitView添加约束——子视图会根据约束,进行等比例的缩放
totalV.translatesAutoresizingMaskIntoConstraints = false//添加约束
let totalV_Lead = totalV.leadingAnchor .constraint(equalTo: self.view.leadingAnchor, constant: 100.0)
let totalV_Trail = totalV.trailingAnchor .constraint(equalTo: self.view.trailingAnchor, constant: -100.0)
let totalV_Top = totalV.topAnchor .constraint(equalTo: self.view.topAnchor, constant: 20.0)
let totalV_Bot = totalV.bottomAnchor .constraint(equalTo: self.view.bottomAnchor, constant: -20)
let totalV_consArr = [totalV_Lead, totalV_Trail, totalV_Top, totalV_Bot]
self.view .addConstraints(totalV_consArr)
效果:对窗口进行缩放时——子视图会根据约束,进行等比例的缩放!当然子视图可以设置其分屏大小!
运行效果 UI界面设置分割条的样式:dividerStyle
属性
totalV.dividerStyle = NSSplitView.DividerStyle.thin//分割条的样式
效果:
UI界面当在NSSplitView的子视图 内部,添加一个固定frame的子视图:(不使用约束布局)
运行效果视图frame固定,布局上不灵活~
func addConstraintWithInnerView(_ outV: NSView) {
let innerV = NSView(frame: NSMakeRect(10 + (100 + 10), 10, 100, 100))//可以不用设置frame,后续会添加约束
outV .addSubview(innerV)
innerV.translatesAutoresizingMaskIntoConstraints = false//添加约束——一直包含在其父视图内部
let innerV_Lead = innerV.leadingAnchor .constraint(lessThanOrEqualTo: outV.leadingAnchor, constant: 10.0)
let innerV_Trail = innerV.trailingAnchor .constraint(equalTo:outV.trailingAnchor, constant: -10.0)
let innerV_Top = innerV.topAnchor .constraint(lessThanOrEqualTo: outV.topAnchor, constant: 20.0)
let innerV_Bot = innerV.bottomAnchor .constraint(equalTo: outV.bottomAnchor, constant: -20.0)
let innerV_consArr = [innerV_Lead, innerV_Trail, innerV_Top, innerV_Bot]
outV .addConstraints(innerV_consArr)
innerV.wantsLayer = true
innerV.layer?.backgroundColor = NSColor.purple.cgColor
}
代码的使用:
//添加上-通过约束布局的子视图
self .addConstraintWithInnerView(leftV)
self .addConstraintWithInnerView(midV)
self .addConstraintWithInnerView(rightV)
效果:对窗口进行缩放时——子视图会根据约束,进行等比例的缩放!当然子视图可以设置其分屏大小,设置其分屏大小后——子视图仍然会根据约束,进行等比例的缩放!
运行效果
为NSSplitView实例设置代理——NSSplitViewDelegate
totalV.delegate = self//设置delegate
部分代理方法的响应:
//MARK:NSSplitViewDelegate
func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
print("shouldAdjustSizeOfSubview \(view.frame)")
return false
}
func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
print("proposedMaximumPosition:", proposedMaximumPosition, "dividerIndex:", dividerIndex)
if dividerIndex == 0 {//首个视图(NSView)最大的尺寸
return 150.0
}
return proposedMaximumPosition
}
func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
print("proposedMaximumPosition:", proposedMinimumPosition, "dividerIndex:", dividerIndex)
if dividerIndex == 0 {//首个视图(NSView)最小的尺寸
return 50.0
}
return proposedMinimumPosition
}
//尺寸变化的通知响应方法
func splitViewDidResizeSubviews(_ notification: Notification) {
//print("notification.object \(notification.object as Any)")
let splitV = notification.object as! NSSplitView//获取(notification.object)到对应的NSSplitView
print("splitViewDidResizeSubviews splitV \(splitV)")
}
func splitViewWillResizeSubviews(_ notification: Notification) {
//print("notification.object \(notification.object as Any)")
let splitV = notification.object as! NSSplitView//获取(notification.object)到对应的NSSplitView
print("splitViewWillResizeSubviews splitV \(splitV)")
}
效果:
123.gif
更多代理方法,参-NSSplitViewDelegate
实现需求 —— 添加一个尺寸不固定的NSTableView
实例对象:
可参考《添加一个尺寸不固定的NSTableView视图》中第3种方法-使用了NSSplitView实例~
参考资料:
NSSplitView | NSSplitViewController | NSSplitViewDelegate
NSSplitView and autolayout
网友评论