Sitemap
Khoa Pham

Khoa Pham

Check out my apps https://onmyway133.com/apps

Member-only story

How to make TextField Stepper in SwiftUI

Jun 20, 2023

Use HStack with TextField and a little extension

extension Binding where Value == Int {
var toString: Binding<String> {
Binding<String>(
get: {
"\(wrappedValue)"
},
set: {
wrappedValue = Int($0) ?? 0
}
)
}
}

struct TextFieldStepper: View {
@Binding var value: Int

var body: some View {
HStack(spacing: 0) {
TextField("", text: $value.toString)
.textFieldStyle(.roundedBorder)
.frame(width: 50)
Stepper("", value: $value)
}
}
}

--

--