Last active
February 13, 2020 23:13
-
-
Save GeekAndDad/d344f69a8b75fffde845952dc1a49731 to your computer and use it in GitHub Desktop.
Sample to show a blog writer a way to do what they wanted (lined up field values) that actually worked. Put this in an iOS app project and you can preview just this view and if you edit the text in the fields you can see it all adjust. Comment in the .border(Color.green) lines to see the frames of the Text views change dynamically as you type :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// bugtest | |
// | |
// Copyright © 2020 Geek & Dad, LLC All rights reserved. | |
// | |
import SwiftUI | |
struct PreferenceTest: View { | |
@State var value1 = "" | |
@State var value2 = "" | |
@State var value3 = "" | |
@State var width: CGFloat? = nil | |
let labelToFieldPadding: CGFloat = 20.0 | |
var body: some View { | |
Form { | |
HStack { | |
Text("1") | |
.frame(width: width, alignment: .leading) | |
.background(ColumnWidthEqualiserView()) | |
// .border(Color.green) | |
TextField("Enter first item", text: $value1) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding([.leading], labelToFieldPadding) | |
} | |
HStack { | |
Text("123456") | |
.frame(width: width, alignment: .leading) | |
.background(ColumnWidthEqualiserView()) | |
// .border(Color.green) | |
TextField("Enter second item", text: $value2) | |
.padding([.leading], labelToFieldPadding) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
} | |
HStack { | |
Text("123") | |
.frame(width: width, alignment: .leading) | |
.background(ColumnWidthEqualiserView()) | |
// .border(Color.green) | |
TextField("Enter third item", text: $value3) | |
.padding([.leading], labelToFieldPadding) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
} | |
}.modifier(ColumnWidth(width: $width)) | |
} | |
} | |
struct ColumnWidthEqualiserView: View { | |
var body: some View { | |
GeometryReader { geometry in | |
Rectangle() | |
.fill(Color.clear) | |
.preference( | |
key: ColumnWidthPreferenceKey.self, | |
value: [ColumnWidthPreference(width: geometry.frame(in: CoordinateSpace.global).width)] | |
) | |
} | |
} | |
} | |
struct ColumnWidthPreference: Equatable { | |
let width: CGFloat | |
} | |
struct ColumnWidthPreferenceKey: PreferenceKey { | |
typealias Value = [ColumnWidthPreference] | |
static var defaultValue: [ColumnWidthPreference] = [] | |
static func reduce(value: inout [ColumnWidthPreference], nextValue: () -> [ColumnWidthPreference]) { | |
value.append(contentsOf: nextValue()) | |
} | |
} | |
struct ColumnWidth: ViewModifier { | |
@Binding var width: CGFloat? | |
func body(content: Content) -> some View { | |
content | |
.onPreferenceChange(ColumnWidthPreferenceKey.self) { preferences in | |
// early in initialization we are called with empty array so guard against that case | |
guard preferences.count > 0 else { return } | |
let newWidth = preferences | |
.map { $0.width } | |
.reduce(0, CGFloat.maximum) | |
// only set width if we actually have a larger value or if it's unset | |
if let w = self.width { | |
if newWidth > w { | |
self.width = newWidth | |
} | |
} else { | |
self.width = newWidth | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
PreferenceTest() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment