Skip to content

Commit eda3526

Browse files
committed
Fix bug demo size by adding a constraint update to the hosting controller
1 parent 2d156df commit eda3526

File tree

4 files changed

+87
-94
lines changed

4 files changed

+87
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// KeyboardView.swift
3+
// Keyboard
4+
//
5+
// Created by Daniel Saidi on 2021-09-25.
6+
//
7+
8+
import SwiftUI
9+
10+
struct KeyboardView: View {
11+
12+
@State private var isCollapsed = false
13+
14+
var body: some View {
15+
stack
16+
}
17+
}
18+
19+
extension KeyboardView {
20+
21+
var grid: some View {
22+
LazyVGrid(columns: [GridItem(.adaptive(minimum: 50, maximum: 100))]) {
23+
ForEach(0...30, id: \.self) { _ in
24+
Color.random.frame(maxHeight: .infinity)
25+
}
26+
}.frame(height: 300)
27+
}
28+
29+
var stack: some View {
30+
VStack {
31+
ForEach(0...3, id: \.self) { _ in
32+
HStack {
33+
ForEach(0...3, id: \.self) { _ in
34+
Color.random
35+
.frame(height: isCollapsed ? 50 : 100)
36+
.onTapGesture { isCollapsed.toggle() }
37+
}
38+
}
39+
}
40+
}//.frame(height: 300)
41+
}
42+
}
43+
44+
extension UIColor {
45+
static var random: UIColor {
46+
return UIColor(
47+
red: .random(in: 0...1),
48+
green: .random(in: 0...1),
49+
blue: .random(in: 0...1),
50+
alpha: 1
51+
)
52+
}
53+
}
54+
55+
extension Color {
56+
static var random: Color {
57+
return Color(
58+
red: .random(in: 0...1),
59+
green: .random(in: 0...1),
60+
blue: .random(in: 0...1)
61+
)
62+
}
63+
}

BugProjects/OrientationBug/Keyboard/KeyboardViewController.swift

+15-89
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,17 @@ import UIKit
99
import KeyboardKit
1010
import SwiftUI
1111

12-
class SizeContext: ObservableObject {
13-
14-
@Published var size: CGSize = .zero
15-
16-
var isLandscape: Bool { size.width > size.height }
17-
}
18-
1912
class KeyboardViewController: KeyboardInputViewController {
2013

21-
private let size = SizeContext()
22-
2314
@IBOutlet var nextKeyboardButton: UIButton!
2415

2516
override func updateViewConstraints() {
2617
super.updateViewConstraints()
27-
size.size = UIScreen.main.bounds.size
2818
// Add custom view sizing constraints here
2919
}
3020

3121
override func viewDidLoad() {
3222
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-
4023
setupNextKeyboardButton()
4124
}
4225

@@ -56,6 +39,15 @@ class KeyboardViewController: KeyboardInputViewController {
5639
*/
5740
}
5841

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+
5951
override func textWillChange(_ textInput: UITextInput?) {
6052
// The app is about to change the document's contents. Perform any preparation here.
6153
}
@@ -65,57 +57,11 @@ class KeyboardViewController: KeyboardInputViewController {
6557
}
6658
}
6759

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-
11460
extension KeyboardViewController {
11561

11662
func setupNextKeyboardButton() {
11763
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: [])
11965
nextKeyboardButton.sizeToFit()
12066
nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
12167
nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
@@ -135,31 +81,11 @@ public class TestHostingController<Content: View>: UIHostingController<Content>
13581
view.translatesAutoresizingMaskIntoConstraints = false
13682
view.leadingAnchor.constraint(equalTo: controller.view.leadingAnchor).isActive = true
13783
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
14085
}
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()
16490
}
16591
}

BugProjects/OrientationBug/OrientationBug.xcodeproj/project.pbxproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
A922B26226FDA47C00E235A1 /* KeyboardKit in Frameworks */ = {isa = PBXBuildFile; productRef = A922B26126FDA47C00E235A1 /* KeyboardKit */; };
1818
A9A9328A26FDD24000E26E56 /* KeyboardViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A9328926FDD24000E26E56 /* KeyboardViewController.swift */; };
1919
A9A9328E26FDD24100E26E56 /* StandardKeyboard.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = A9A9328726FDD24000E26E56 /* StandardKeyboard.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
20+
A9A9329826FFC9FD00E26E56 /* KeyboardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A9329726FFC9FD00E26E56 /* KeyboardView.swift */; };
2021
/* End PBXBuildFile section */
2122

2223
/* Begin PBXContainerItemProxy section */
@@ -63,6 +64,7 @@
6364
A9A9328726FDD24000E26E56 /* StandardKeyboard.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = StandardKeyboard.appex; sourceTree = BUILT_PRODUCTS_DIR; };
6465
A9A9328926FDD24000E26E56 /* KeyboardViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardViewController.swift; sourceTree = "<group>"; };
6566
A9A9328B26FDD24100E26E56 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
67+
A9A9329726FFC9FD00E26E56 /* KeyboardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardView.swift; sourceTree = "<group>"; };
6668
/* End PBXFileReference section */
6769

6870
/* Begin PBXFrameworksBuildPhase section */
@@ -135,6 +137,7 @@
135137
A922B25226FDA1A000E235A1 /* Keyboard */ = {
136138
isa = PBXGroup;
137139
children = (
140+
A9A9329726FFC9FD00E26E56 /* KeyboardView.swift */,
138141
A922B25326FDA1A000E235A1 /* KeyboardViewController.swift */,
139142
A922B25526FDA1A000E235A1 /* Info.plist */,
140143
);
@@ -305,6 +308,7 @@
305308
buildActionMask = 2147483647;
306309
files = (
307310
A922B25426FDA1A000E235A1 /* KeyboardViewController.swift in Sources */,
311+
A9A9329826FFC9FD00E26E56 /* KeyboardView.swift in Sources */,
308312
);
309313
runOnlyForDeploymentPostprocessing = 0;
310314
};
@@ -658,8 +662,8 @@
658662
isa = XCRemoteSwiftPackageReference;
659663
repositoryURL = "[email protected]:KeyboardKit/KeyboardKit.git";
660664
requirement = {
661-
kind = upToNextMajorVersion;
662-
minimumVersion = 4.0.0;
665+
branch = "bug-fixes";
666+
kind = branch;
663667
};
664668
};
665669
/* End XCRemoteSwiftPackageReference section */

BugProjects/OrientationBug/OrientationBug.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"package": "KeyboardKit",
66
"repositoryURL": "[email protected]:KeyboardKit/KeyboardKit.git",
77
"state": {
8-
"branch": null,
9-
"revision": "0f02c975055d5773b8be80fb050451f99a3fd5c3",
10-
"version": "4.9.0"
8+
"branch": "bug-fixes",
9+
"revision": "d836afa606112fb8bc59c83f4ff81a2e7a1eaf9e",
10+
"version": null
1111
}
1212
}
1313
]

0 commit comments

Comments
 (0)