Skip to content

Commit 4b1e271

Browse files
committed
Fix sheets under Mac Catalyst (content alignment + escape-to-dismiss)
1 parent 74b2b3a commit 4b1e271

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ disabled_rules:
1111
- file_length
1212
- force_cast # Required a lot in backend implementations
1313
- function_body_length
14+
- function_parameter_count
1415

1516
line_length: 140
1617
type_body_length: 400

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ extension UIKitBackend {
1313
#endif
1414

1515
let contentView = UIView()
16+
// Fetch the child controller before adding the child to the view
17+
// hierarchy. Otherwise, if the child doesn't have its own controller, we'd
18+
// get back a reference to the sheet controller and attempt to add it as a
19+
// child of itself.
1620
if let childController = content.controller {
1721
sheet.addChild(childController)
1822
}
@@ -22,9 +26,8 @@ extension UIKitBackend {
2226
equalTo: content.view.topAnchor,
2327
constant: 0
2428
)
25-
sheet.leadingConstraint = contentView.leadingAnchor.constraint(
26-
equalTo: content.view.leadingAnchor,
27-
constant: 0
29+
sheet.leadingConstraint = contentView.centerXAnchor.constraint(
30+
equalTo: content.view.centerXAnchor
2831
)
2932
sheet.topConstraint?.isActive = true
3033
sheet.leadingConstraint?.isActive = true
@@ -46,15 +49,6 @@ extension UIKitBackend {
4649
backgroundColor: Color?,
4750
interactiveDismissDisabled: Bool
4851
) {
49-
// Center the sheet's content horizontally
50-
#if os(tvOS)
51-
let leadingPadding = (window.frame.size.width - CGFloat(size.x)) / 2
52-
sheet.leadingConstraint!.constant = -leadingPadding
53-
#else
54-
let leadingPadding = (sheet.preferredContentSize.width - CGFloat(size.x)) / 2
55-
sheet.leadingConstraint!.constant = leadingPadding
56-
#endif
57-
5852
sheet.onDismiss = onDismiss
5953
setPresentationDetents(of: sheet, to: detents)
6054
setPresentationCornerRadius(of: sheet, to: cornerRadius)
@@ -242,6 +236,20 @@ public final class CustomSheet: UIViewController {
242236
dismiss(animated: true)
243237
}
244238

239+
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
240+
// Support escape-to-dismiss for iPadOS and Mac Catalyst
241+
var unhandledPresses: Set<UIPress> = []
242+
for press in presses {
243+
if let key = press.key, key.keyCode == .keyboardEscape {
244+
dismiss(animated: true)
245+
} else {
246+
unhandledPresses.insert(press)
247+
}
248+
}
249+
250+
super.pressesBegan(unhandledPresses, with: event)
251+
}
252+
245253
public override func viewDidDisappear(_ animated: Bool) {
246254
super.viewDidDisappear(animated)
247255

0 commit comments

Comments
 (0)