Skip to content

Commit a6a29ca

Browse files
committed
Bypass view updates without the runloop tick.
1 parent 01b7e28 commit a6a29ca

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Sources/ResponsiveTextField/ResponsiveTextField.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public struct ResponsiveTextField {
7575
/// Return `true` to allow the change or `false` to prevent the change.
7676
var shouldChange: ((String, String) -> Bool)?
7777

78+
fileprivate var shouldUpdateView: Bool = true
79+
7880
public init(
7981
placeholder: String,
8082
text: Binding<String>,
@@ -94,6 +96,12 @@ public struct ResponsiveTextField {
9496
self.handleDelete = handleDelete
9597
self.shouldChange = shouldChange
9698
}
99+
100+
mutating func skippingViewUpdates(_ callback: () -> Void) {
101+
shouldUpdateView = false
102+
callback()
103+
shouldUpdateView = true
104+
}
97105
}
98106

99107
// MARK: - UIViewRepresentable
@@ -126,6 +134,8 @@ extension ResponsiveTextField: UIViewRepresentable {
126134
}
127135

128136
public func updateUIView(_ uiView: UITextField, context: Context) {
137+
guard shouldUpdateView else { return }
138+
129139
uiView.isEnabled = isEnabled
130140
uiView.isSecureTextEntry = isSecure
131141
uiView.returnKeyType = returnKeyType
@@ -142,7 +152,7 @@ extension ResponsiveTextField: UIViewRepresentable {
142152
}
143153

144154
public class Coordinator: NSObject, UITextFieldDelegate {
145-
let parent: ResponsiveTextField
155+
var parent: ResponsiveTextField
146156

147157
@Binding
148158
var text: String
@@ -158,18 +168,12 @@ extension ResponsiveTextField: UIViewRepresentable {
158168

159169
public func textFieldDidBeginEditing(_ textField: UITextField) {
160170
guard !isEditing else { return }
161-
162-
// Scheduled on the next runloop to avoid runtime warnings
163-
// about changing state during a view update.
164-
RunLoop.main.schedule { self.isEditing = true }
171+
parent.skippingViewUpdates { self.isEditing = true }
165172
}
166173

167174
public func textFieldDidEndEditing(_ textField: UITextField) {
168175
guard isEditing else { return }
169-
170-
// Scheduled on the next runloop to avoid runtime warnings
171-
// about changing state during a view update.
172-
RunLoop.main.schedule { self.isEditing = false }
176+
parent.skippingViewUpdates { self.isEditing = false }
173177
}
174178

175179
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {

0 commit comments

Comments
 (0)