Skip to content

Commit 4ac3a74

Browse files
committed
fix layout shift when switching to fcitx
1 parent cd20a4f commit 4ac3a74

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

keyboard/KeyboardViewController.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
5555
startFcitx(appBundlePath, "\(Bundle.main.bundlePath)/share", appGroup.path)
5656

5757
hostingController = UIHostingController(rootView: virtualKeyboardView)
58-
addChild(hostingController)
59-
6058
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
6159

6260
// Spotlight shows that system keyboard has transparent background.
6361
hostingController.view.backgroundColor = .clear
6462
view.backgroundColor = .clear
63+
}
64+
65+
override func viewWillAppear(_ animated: Bool) {
66+
logger.info("viewWillAppear \(self.id)")
6567

68+
// If setting view in viewDidLoad instead, it will cause huge layout shift.
69+
addChild(hostingController)
6670
view.addSubview(hostingController.view)
6771

6872
NSLayoutConstraint.activate([
@@ -73,10 +77,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
7377
])
7478

7579
hostingController.didMove(toParent: self)
76-
}
7780

78-
override func viewWillAppear(_ animated: Bool) {
79-
logger.info("viewWillAppear \(self.id)")
8081
virtualKeyboardView.setReturnKeyType(textDocumentProxy.returnKeyType)
8182
super.viewWillAppear(animated)
8283
let keyboard = Bundle.main.bundleURL.deletingPathExtension().lastPathComponent
@@ -92,13 +93,13 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
9293
logger.info("viewWillDisappear \(self.id)")
9394
super.viewWillDisappear(animated)
9495
focusOut(self)
96+
hostingController.willMove(toParent: nil)
97+
hostingController.view.removeFromSuperview()
98+
hostingController.removeFromParent()
9599
}
96100

97101
deinit {
98102
logger.info("deinit \(self.id)")
99-
hostingController.willMove(toParent: nil)
100-
hostingController.view.removeFromSuperview()
101-
hostingController.removeFromParent()
102103
hostingController = nil
103104
}
104105

0 commit comments

Comments
 (0)