@@ -9,34 +9,17 @@ import UIKit
9
9
import KeyboardKit
10
10
import SwiftUI
11
11
12
- class SizeContext : ObservableObject {
13
-
14
- @Published var size : CGSize = . zero
15
-
16
- var isLandscape : Bool { size. width > size. height }
17
- }
18
-
19
12
class KeyboardViewController : KeyboardInputViewController {
20
13
21
- private let size = SizeContext ( )
22
-
23
14
@IBOutlet var nextKeyboardButton : UIButton !
24
15
25
16
override func updateViewConstraints( ) {
26
17
super. updateViewConstraints ( )
27
- size. size = UIScreen . main. bounds. size
28
18
// Add custom view sizing constraints here
29
19
}
30
20
31
21
override func viewDidLoad( ) {
32
22
super. viewDidLoad ( )
33
-
34
- // This is the hosting controller from KeyboardKit
35
- // KeyboardHostingController(rootView: grid)
36
- // This is the hosting controller from further down.
37
- TestHostingController ( rootView: KeyboardView ( size: size) )
38
- . add ( to: self )
39
-
40
23
setupNextKeyboardButton ( )
41
24
}
42
25
@@ -56,6 +39,15 @@ class KeyboardViewController: KeyboardInputViewController {
56
39
*/
57
40
}
58
41
42
+ override func viewWillSetupKeyboard( ) {
43
+ super. viewWillSetupKeyboard ( )
44
+ // This is the hosting controller from KeyboardKit
45
+ // KeyboardHostingController(rootView: grid)
46
+ // This is the hosting controller from further down.
47
+ TestHostingController ( rootView: KeyboardView ( ) )
48
+ . add ( to: self )
49
+ }
50
+
59
51
override func textWillChange( _ textInput: UITextInput ? ) {
60
52
// The app is about to change the document's contents. Perform any preparation here.
61
53
}
@@ -65,57 +57,11 @@ class KeyboardViewController: KeyboardInputViewController {
65
57
}
66
58
}
67
59
68
- extension GeometryProxy {
69
-
70
- var isLandscape : Bool {
71
- size. width > size. height
72
- }
73
- }
74
-
75
- struct KeyboardView : View {
76
-
77
- init ( size: SizeContext ) {
78
- _size = ObservedObject ( wrappedValue: size)
79
- }
80
-
81
- @ObservedObject private var size : SizeContext
82
-
83
- var body : some View {
84
- stack
85
- }
86
- }
87
-
88
- extension KeyboardView {
89
-
90
- var grid : some View {
91
- LazyVGrid ( columns: [ GridItem ( . adaptive( minimum: 50 , maximum: 100 ) ) ] ) {
92
- ForEach ( 0 ... 30 , id: \. self) { _ in
93
- Color . random. frame ( maxHeight: . infinity)
94
- }
95
- } . frame ( height: 300 )
96
- }
97
-
98
- var stack : some View {
99
- VStack {
100
- ForEach ( 0 ... 3 , id: \. self) { _ in
101
- HStack {
102
- ForEach ( 0 ... 3 , id: \. self) { _ in
103
- Color . random
104
- . frame ( height: size. isLandscape ? 50 : 100 )
105
- . overlay ( Text ( " \( Int ( size. size. width) ) , \( Int ( size. size. height) ) " ) )
106
- }
107
- }
108
- }
109
- } //.frame(height: 300)
110
- }
111
- }
112
-
113
-
114
60
extension KeyboardViewController {
115
61
116
62
func setupNextKeyboardButton( ) {
117
63
nextKeyboardButton = UIButton ( type: . system)
118
- nextKeyboardButton. setTitle ( NSLocalizedString ( " Next Keyboard \( size . size ) " , comment: " Title for 'Next Keyboard' button " ) , for: [ ] )
64
+ nextKeyboardButton. setTitle ( NSLocalizedString ( " Next Keyboard " , comment: " Title for 'Next Keyboard' button " ) , for: [ ] )
119
65
nextKeyboardButton. sizeToFit ( )
120
66
nextKeyboardButton. translatesAutoresizingMaskIntoConstraints = false
121
67
nextKeyboardButton. addTarget ( self , action: #selector( handleInputModeList ( from: with: ) ) , for: . allTouchEvents)
@@ -135,31 +81,11 @@ public class TestHostingController<Content: View>: UIHostingController<Content>
135
81
view. translatesAutoresizingMaskIntoConstraints = false
136
82
view. leadingAnchor. constraint ( equalTo: controller. view. leadingAnchor) . isActive = true
137
83
view. trailingAnchor. constraint ( equalTo: controller. view. trailingAnchor) . isActive = true
138
- view. topAnchor. constraint ( equalTo: controller. view. topAnchor) . isActive = true
139
- view. bottomAnchor. constraint ( equalTo: controller. view. bottomAnchor) . isActive = true
84
+ view. heightAnchor. constraint ( equalTo: controller. view. heightAnchor) . isActive = true
140
85
}
141
- }
142
-
143
-
144
-
145
-
146
- extension UIColor {
147
- static var random : UIColor {
148
- return UIColor (
149
- red: . random( in: 0 ... 1 ) ,
150
- green: . random( in: 0 ... 1 ) ,
151
- blue: . random( in: 0 ... 1 ) ,
152
- alpha: 1
153
- )
154
- }
155
- }
156
-
157
- extension Color {
158
- static var random : Color {
159
- return Color (
160
- red: . random( in: 0 ... 1 ) ,
161
- green: . random( in: 0 ... 1 ) ,
162
- blue: . random( in: 0 ... 1 )
163
- )
86
+
87
+ public override func viewWillLayoutSubviews( ) {
88
+ super. viewWillLayoutSubviews ( )
89
+ updateViewConstraints ( )
164
90
}
165
91
}
0 commit comments