【发布时间】:2016-01-22 01:04:15
【问题描述】:
我正在开发一个可可应用程序。它包含一个带有一些功能按钮的工具栏。就像里德一样。
我想在调整拆分视图大小的同时调整工具栏部分的大小。如下所示。如何实现这种功能?
任何人可以帮助我或提供一些建议将不胜感激。
我正在使用 XCode7、Swift 和 Storyboard 进行开发。
【问题讨论】:
标签: xcode cocoa autolayout nssplitview nstoolbar
我正在开发一个可可应用程序。它包含一个带有一些功能按钮的工具栏。就像里德一样。
我想在调整拆分视图大小的同时调整工具栏部分的大小。如下所示。如何实现这种功能?
任何人可以帮助我或提供一些建议将不胜感激。
我正在使用 XCode7、Swift 和 Storyboard 进行开发。
【问题讨论】:
标签: xcode cocoa autolayout nssplitview nstoolbar
【讨论】:
self.window!.titleVisibility = NSWindow.TitleVisibility.hidden self.window!.titlebarAppearsTransparent = true self.window!.styleMask.insert([.fullSizeContentView ])
我已经为 Swift 3 调整了 livingstonef 的实现,还添加了缺少的 NSBezierPath 扩展:
import Cocoa
@IBDesignable class ToolbarCustomView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
//The background
let startingColor = NSColor(red: 232/256, green: 230/256, blue: 232/256, alpha: 1)
let endingColor = NSColor(red: 209/256, green: 208/256, blue: 209/256, alpha: 1)
let gradient = NSGradient(starting: startingColor, ending: endingColor)
gradient?.draw(in: self.bounds, angle: 270)
//The bottom border
let borderPath = NSBezierPath()
let startingPoint = NSPoint(x: dirtyRect.origin.x, y: 0)
let stoppingPoint = NSPoint(x: dirtyRect.width, y: 0)
borderPath.move(to: startingPoint)
borderPath.line(to: stoppingPoint)
let shapeLayer = CAShapeLayer()
self.layer?.addSublayer(shapeLayer)
shapeLayer.path = borderPath.cgPath
shapeLayer.strokeColor = NSColor(red: 180/256, green: 182/256, blue: 180/256, alpha: 0.6).cgColor
shapeLayer.fillColor = .clear
shapeLayer.lineWidth = 1
}
}
extension NSBezierPath {
public var cgPath: CGPath {
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< self.elementCount {
let type = self.element(at: i, associatedPoints: &points)
switch type {
case .moveToBezierPathElement:
path.move(to: points[0])
case .lineToBezierPathElement:
path.addLine(to: points[0])
case .curveToBezierPathElement:
path.addCurve(to: points[2], control1: points[0], control2: points[1])
case .closePathBezierPathElement:
path.closeSubpath()
}
}
return path
}
}
【讨论】:
一切都与约束有关
如果工具栏在拆分视图中:
在您的工具栏上设置约束“与最近邻的间距”,例如 0 表示左右 然后该按钮还必须与工具栏有一个“到最近邻居的间距”,例如右侧的 8
编辑:在此处查看按钮以添加约束http://oi63.tinypic.com/2s7szgi.jpg
【讨论】: