· 6 years ago · Sep 14, 2019, 06:24 PM
1## Toolbar
2
3NSToolbar (Mac API)
4UIWindowScene.titlebar.toolbar — access
5
6```swift
7func scene(_ scene: UIScene,
8 willConnectTo session: UISceneSession,
9 options connectionOptions: UIScene.ConnectionOptions) {
10 // setup the window
11
12 #if targetEnvironment(UIKitForMac)
13
14 if let windowScene = scene as? UIWindowScene {
15 if let titlebar = windowScene.titlebar {
16 let toolbar = NSToolbar(identifier: "myIdentifier")
17
18 // configure the toolbar, add items to it
19
20 titlebar.toolbar = toolbar
21 }
22 }
23 #endif
24 }
25}
26```
27
28Quite different from iOS, where toolbar is usually at the bottom.
29
30---
31
32## Sidebar
33
34UISplitViewController (old API in new look)
35new `primaryBackgroundStyle` property (can be `.none` or `.sidebar`)
36When it is `.sidebar`, the embedded table view changes it's appearance as well (styles `.grouped` and `.insetGrouped`)
37
38In UISplitViewController subclass:
39
40```swift
41override func viewDidLoad() {
42 super.viewDidLoad()
43
44 #if targetEnvironment(UIKitForMac)
45 primaryBackgroundStyle = .sidebar
46 #else
47 preferredDisplayMode = .allVisible
48 #endif
49
50```
51
52The sidebar even has a translucensy. It is REALLY a sidebar: we can change icon size in Systen Preferences!
53
54---
55
56## Touch Bar
57
58Class `NSTouchBar` (Mac API exposed to UIKit)
59
60Access via
61`UIResponder.touchbar`
62`UIViewController.childViewControllerForTouchBar`
63`UIViewController.setNeedsTouchBarUpdate`
64
65---
66
67## Preferences Window
68
69If 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.
70
71---
72
73## Context Menu
74
75* UIContextMenuInteraction
76
77---
78
79## App icon
80
81macOS icons can be larger and have transparency (have custom shape thanks to transparency)
82
83---
84
85## Table View rows reordering
86
87Implement corresponding callbacks and you can drag to reorder rows without going into Edit mode
88
89---
90
91## Hover
92
93new 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)
94
95## Data protection
96
97iOS: NSData write with encryption
98`data.write(to: file, options: .completeFileProtection`
99
100It compiles on macOS but without protection )
101We can rely in FileVault ot use new CryptoKit
102
103```
104let key = SymmetricKey(size: .bits256)
105let sealed = try AES.GCM.seal(data, using: key)
106// write file to disk
107// write key to the keychain
108```
109
110## Menu Bar
111
112* UIKeyCommand
113* UICommand
114* UIMenu
115* UIMenuBuilder
116
117We can use as is, basic one. We can customize it in Interface Builder and we can customize it in code for text fields
118
119In AppDelegate:
120
121```swift
122
123override func buildCommands(with builder: UICommandBuilder) {
124 guard builder.system == .main else { return }
125
126 // we don't need `Format` menu in Main menu,
127 // so let's remove it
128 builder.remove(menu: .format)
129
130 // let's add
131 let addNoteCommand = UIKeyCommand(title: "New Note…",
132 action: #selector(...),
133 input: "f",
134 modifierFlags: [.command, .alternate])
135 let menu = UIMenu<UICommand>.create(title: "",
136 image: nil,
137 identifier(UIMenuIdentifier(""),
138 options: .displayInline),
139 children: [addNoteCommand])
140
141 builder.insertChild(menu, atStartOfMenu: .file)
142
143```
144
145It works both on Mac and iPad with keypad