Latest 0.1.1
Homepage https://github.com/hechen/CocoaNavigationController
License MIT Permission is hereby granted …
Platforms osx 10.13, requires ARC
Authors

Build Status Cocoapods Cocoapods platforms Swift Version

UINavigationController alike, macOS

Demo

How to use

    let navigationController = CocoaNavigationController(withFrame: NSApp.keyWindow!.frame, rootViewController: nil)
    let window = NSWindow(contentViewController: self.navigationController!)
    window.makeKeyAndOrderFront(nil)

    // Push
    let vc = TestViewController(nibName: "TestViewController", bundle: nil)
    navigationController.pushViewController(vc, animated: true)

    // Pop
    navigationController?.popViewController(vc, animated: true)

Under the hood

Actually, we just transition between two subview, contained by root view.

Arrange the two subview horizontally. Then slide the whole one from right to left for push, pop with opposite direction.


                        Push
     ◀─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
     ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

     │                   │ │                   │

     │                   │ │                   │

     │       From        │ │        To         │

     │                   │ │                   │

     │                   │ │                   │

     └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
     ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶
                        Pop

For transition fluently, we snapshot from-view and to-view as placeholders, which hide all the remove/add actions.

And, like drawViewHierarchyInRect:afterScreenUpdates in iOS, NSView support similar method like below:

extension NSView {
    func snapshot() -> NSImage? {
        // Returns a bitmap-representation object suitable for caching the specified portion of the view.
        guard let bitmapRep = bitmapImageRepForCachingDisplay(in: bounds) else { return nil }
        cacheDisplay(in: bounds, to: bitmapRep)
        let image = NSImage()
        image.addRepresentation(bitmapRep)
        bitmapRep.size = bounds.size
        return image
    }
}

Latest podspec

{
    "name": "CocoaNavigationController",
    "version": "0.1.1",
    "license": {
        "type": "MIT",
        "file": "LICENSE",
        "text": "Permission is hereby granted ..."
    },
    "summary": "UINavigationController Alike On macOS",
    "homepage": "https://github.com/hechen/CocoaNavigationController",
    "authors": {
        "Chen": "[email protected]"
    },
    "source": {
        "git": "https://github.com/hechen/CocoaNavigationController.git",
        "tag": "0.1.1"
    },
    "source_files": "Source/**/**/*",
    "platforms": {
        "osx": "10.13"
    },
    "requires_arc": true,
    "swift_version": "5.0"
}

Pin It on Pinterest

Share This