UINavigationController for macOS (Swift, Objective-C)
Looking for macOS (Mac OS X) analog of UIKit's UINavigationController
from iOS? This class mimics its behavior.
Attention: Navigation bar is not implemented. All methods must be called from main thread.
Swift version (3.0): KSNavigationController/Swift
ObjC version: KSNavigationController/ObjectiveC
For Swift 2.2 support see version 0.1.
// Swift
let vc1 = TestViewController()
let navVC = KSNavigationController(rootViewController: vc1)
navVC?.view.frame = NSMakeRect(0.0, 0.0, 480.0, 272.0) // Or use constraints if appropriate
self.window.contentViewController = navVC
self.window.orderFrontRegardless()
// ObjC
TestViewController *vc1 = [[TestViewController alloc] init];
KSNavigationController *navVC = [[KSNavigationController alloc] initWithRootViewController:vc1];
navVC.view.frame = NSMakeRect(0.0, 0.0, 480.0, 272.0); // Or use constraints if appropriate
self.window.contentViewController = navVC;
[self.window orderFrontRegardless];
Here your TestViewController
class is a subclass of NSViewController
. It also has to conform to KSNavigationControllerCompatible
protocol in order to have access to navigationController
property.
Now, inside your NSViewController
you can access navigationController
property (just like in iOS) and push any new view controller on top of navigation stack:
// Swift
@IBAction func pushAction(sender: AnyObject) {
self.navigationController?.pushViewController(TestViewController(), animated: true)
}
// ObjC
- (IBAction)pushAction:(id)sender {
[self.navigationController pushViewController:[[TestViewController alloc] init] animated:YES];
}
Do the following to pop the top view controller from stack:
// Swift
self.navigationController?.popViewControllerAnimated(true)
// ObjC
[self.navigationController popViewControllerAnimated:YES];
Conform to this protocol if you want your NSViewController
subclass to work with KSNavigationController
.
This protocol has only one property:
/*Swift*/ weak var navigationController: KSNavigationController? {get set}
/*ObjC*/ @property (weak, nonatomic) KSNavigationController *navigationController;
Warning: Do not set this properly by yourself.
Objective-C only:
You must synthesize navigationController
property explicitly in your subclass implementation:
@synthesize navigationController = _navigationController;
See example projects for more understanding.
Published under MIT license.
Copyright (c) 2016 A. Gordiyenko.