Skip to content

Instantly share code, notes, and snippets.

@aronskaya
Created September 3, 2019 16:51
Show Gist options
  • Save aronskaya/dcfad2928ace8393f262b6b153733576 to your computer and use it in GitHub Desktop.
Save aronskaya/dcfad2928ace8393f262b6b153733576 to your computer and use it in GitHub Desktop.
Notes on Catalyst (macOS)

Toolbar

NSToolbar (Mac API) UIWindowScene.titlebar.toolbar — access

func scene(_ scene: UIScene,
			willConnectTo session: UISceneSession,
			options connectionOptions: 				UIScene.ConnectionOptions) {
		// setup the window
		
		#if targetEnvironment(UIKitForMac)
		
		if let windowScene = scene as? UIWindowScene {
			if let titlebar = windowScene.titlebar {
				let toolbar = NSToolbar(identifier: "myIdentifier")
				
				// configure the toolbar, add items to it
				
				titlebar.toolbar = toolbar
			}
		}
		#endif
	}
}

Quite different from iOS, where toolbar is usually at the bottom.


Sidebar

UISplitViewController (old API in new look) new primaryBackgroundStyle property (can be .none or .sidebar) When it is .sidebar, the embedded table view changes it's appearance as well (styles .grouped and .insetGrouped)

In UISplitViewController subclass:

override func viewDidLoad() {
	super.viewDidLoad()
	
	#if targetEnvironment(UIKitForMac)
	primaryBackgroundStyle = .sidebar
	#else
	preferredDisplayMode = .allVisible
	#endif

The sidebar even has a translucensy. It is REALLY a sidebar: we can change icon size in Systen Preferences!


Touch Bar

Class NSTouchBar (Mac API exposed to UIKit)

Access via UIResponder.touchbar UIViewController.childViewControllerForTouchBar UIViewController.setNeedsTouchBarUpdate


Preferences Window

If you have a Settings.bundle in iOS app, you automatically get a Preferences Window, that is opened with ⌘, keys. It is also present in the Main Menu.


Context Menu

  • UIContextMenuInteraction

App icon

macOS icons can be larger and have transparency (have custom shape thanks to transparency)


Table View rows reordering

Implement corresponding callbacks and you can drag to reorder rows without going into Edit mode


Hover

new UIHoverGestureRecognizer is used to know when the mouse is positioned over a view (buttons ans other controls can be slightly highlighted on mouse over — hovered)

Data protection

iOS: NSData write with encryption data.write(to: file, options: .completeFileProtection

It compiles on macOS but without protection ) We can rely in FileVault ot use new CryptoKit

let key = SymmetricKey(size: .bits256)
let sealed = try AES.GCM.seal(data, using: key)
// write file to disk
// write key to the keychain

Menu Bar

  • UIKeyCommand
  • UICommand
  • UIMenu
  • UIMenuBuilder

We can use as is, basic one. We can customize it in Interface Builder and we can customize it in code for text fields

In AppDelegate:

override func buildCommands(with builder: UICommandBuilder) {
	guard builder.system == .main else { return } 
	
	// we don't need `Format` menu in Main menu,
	// so let's remove it
	builder.remove(menu: .format) 
	
	// let's add 
	let addNoteCommand = UIKeyCommand(title: "New Note…",
							action: #selector(...),
							input: "f",
							modifierFlags: [.command, .alternate])
	let menu = UIMenu<UICommand>.create(title: "",
									image: nil,
									identifier(UIMenuIdentifier(""),
									options: .displayInline),
									children: [addNoteCommand])
					
	builder.insertChild(menu, atStartOfMenu: .file)

It works both on Mac and iPad with keypad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment