Vertical Sidebar
xib-based
Drag NSView control to xib interface from control libraries, change control custom class to component class name of your exported.
Implement style configuration and data initialization in the controller's viewDidLoad method, the detailed code is as follows:
import Cocoa
class ViewController: NSViewController {
var gridItems:[HDSideBarItem] = [HDSideBarItem]()
@IBOutlet var sideBar: HDSideBar!
@IBOutlet var textView: HDTextView!
override func viewDidLoad() {
super.viewDidLoad()
configSideBarStyle()
configSideBarData()
}
func configSideBarData() {
let item0 = HDSideBarButtonItem(title: "", image: NSImage(named: "Rect"), altImage: NSImage(named: "RectHL"))
let item1 = HDSideBarButtonItem(title: "Swift", image: NSImage(named: "Time"), altImage: NSImage(named: "TimeHL"))
let item2 = HDSideBarButtonItem(title: "Objective-C", image: NSImage(named: "Watch"), altImage: NSImage(named: "Watch"))
let separator = HDSideBarFlexibleSeparatorItem()
let item3 = HDSideBarButtonItem(title: "Objective-C", image: NSImage(named: "Trash"),hasSelectBorder:false)
gridItems = [item1,item0,item2,separator,item3]
sideBar.dataSource = self
sideBar.delegate = self
sideBar.reloadData()
// sideBar.updateBadge(number: 20, atButtonIndex: 1)
}
func configSideBarStyle() {
self.sideBar.itemSize = NSSize(width: 40.0, height: 40.0)
self.sideBar.itemSpacing = 10.0
self.sideBar.fillGradient = NSGradient.gradient(colorsStr: "rgba(14,68,186,1.0)-rgba(17,88,197,1.0)", locationsStr: "0.0-1.0")
self.sideBar.borderMask = [.top,.bottom,.left,.right]
self.sideBar.borderColor = NSColor(calibratedRed: 0.00, green: 0.00, blue: 0.00, alpha: 1.0)
self.sideBar.borderWidth = 0.5
self.sideBar.itemTintColor = NSColor(calibratedRed: 0.27, green: 0.84, blue: 0.97, alpha: 1.0)
self.sideBar.itemImageColor = NSColor(calibratedRed: 0.07, green: 0.52, blue: 0.46, alpha: 1.0)
self.sideBar.buttonCellBorderWidth = 0.5
self.sideBar.itemAllowTintColor = true
self.sideBar.itemAllowDisplayBorder = false
}
}
extension ViewController: HDSideBarDataSource {
func numberOfItems(_ sideBar: HDSideBar) -> Int {
return gridItems.count
}
func sideBar(_ sideBar: HDSideBar, itemOfIndex index: Int) -> AnyObject? {
return gridItems[index]
}
}
code-based
Declare lazy load variable in the controller class and add it to the view, The same way to implement the style configuration and data initialization in the controller's viewDidLoad method.
import Cocoa
class ViewController: NSViewController {
lazy var sideBar: HDSideBar = {
let sideBar = HDSideBar(frame: NSRect(x: 100, y: 100, width: 50, height: 294))
return sideBar
}()
var gridItems:[HDSideBarItem] = [HDSideBarItem]()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(sideBar)
configSideBarStyle()
configSideBarData()
}
func configSideBarData() {
let item0 = HDSideBarButtonItem(title: "", image: NSImage(named: "Rect"), altImage: NSImage(named: "RectHL"))
let item1 = HDSideBarButtonItem(title: "Swift", image: NSImage(named: "Time"), altImage: NSImage(named: "TimeHL"))
let item2 = HDSideBarButtonItem(title: "Objective-C", image: NSImage(named: "Watch"), altImage: NSImage(named: "Watch"))
let separator = HDSideBarFlexibleSeparatorItem()
let item3 = HDSideBarButtonItem(title: "Objective-C", image: NSImage(named: "Trash"),hasSelectBorder:false)
gridItems = [item1,item0,item2,separator,item3]
sideBar.dataSource = self
sideBar.delegate = self
sideBar.reloadData()
sideBar.updateBadge(number: 20, atButtonIndex: 1)
}
func configSideBarStyle() {
self.sideBar.itemSize = NSSize(width: 40.0, height: 40.0)
self.sideBar.itemSpacing = 10.0
self.sideBar.fillColor = NSColor(calibratedRed: 1.00, green: 1.00, blue: 1.00, alpha: 1.0)
self.sideBar.borderMask = [.top,.bottom,.left,.right]
self.sideBar.borderColor = NSColor(calibratedRed: 0.00, green: 0.00, blue: 0.00, alpha: 1.0)
self.sideBar.borderWidth = 0.5
self.sideBar.itemTintColor = NSColor(calibratedRed: 0.02, green: 0.26, blue: 0.76, alpha: 1.0)
self.sideBar.itemImageColor = NSColor(calibratedRed: 0.43, green: 0.43, blue: 0.43, alpha: 1.0)
self.sideBar.buttonCellBorderWidth = 0.5
self.sideBar.itemAllowTintColor = true
self.sideBar.itemAllowDisplayBorder = false
}
}
extension ViewController: HDSideBarDataSource {
func numberOfItems(_ sideBar: HDSideBar) -> Int {
return gridItems.count
}
func sideBar(_ sideBar: HDSideBar, itemOfIndex index: Int) -> AnyObject? {
return gridItems[index]
}
}