A UINavigationController port for Cocoa development
To integrate CKNavigation into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :osx, '10.12'
target 'TargetName' do
pod 'CKNavigation', '~> 0.1.1'
end
The CKNavigationController
is meant to provide navigation in a single NSWindow
similar to that of UIKit's UINavigationController
on iOS. This is a great solution for seperating views in low profile status bar apps and the like.
Implementing a CKNavigationController
is exactly like UINavigationController
in iOS. Simply call the initializer and pass in the controller you'd like to set as root. Note: in order to add a view controller to a navigation controller, the view controller must subclass CKNavigatableViewController
or explicitly conform to the CKNavigatable
protocol.
import CKNavigation
let myController = MyViewController()
let myNavigationController = CKNavigationController(rootViewController: myController)
This approach is well suited for programmatic user interfaces. You could simply create an NSWindow
instance, and add your navigation controller as a subview.
import Foundation
import Cocoa
import CKNavigation
class AppDelegate: NSObject, NSApplicationDelegate {
var navigationController: CKNavigationController!
let controller = ViewController()
let window: NSWindow {
let content = NSRect(x: 0, y: 0, width: 500, height: 500)
let window = NSWindow(contentRect: content, styleMask: .closable, backing: .buffered, defer: false)
return window
}()
func applicationDidFinishLaunching(_ aNotification: Notification) {
// initialize navigation controller with root view
navigationController = CKNavigationController(rootViewController: controller)
// add navigation controller to the window
window.contentView?.addSubview(navigationController.view)
navigationController.view.wantsLayer = true
window.makeKeyAndOrderFront(nil)
}
}
Pushing a view controller to the stack is done easily, too. From inside the myViewController
class:
@objc func handleNextButtonPress(_ sender: Any?) {
let newController = AnotherViewController()
self.navigationController!.pushViewController(newController)
}
Similarly, to pop a view controller from the stack:
@objc func handlePreviousButtonPress(_ sender: Any?) {
self.navigationController!.popViewController()
}
The example that you see in the demo gif, at the top is available here. This implementation was 100% programmatic.
MIT © Charles Kenney