blog
blog copied to clipboard
🍁 What you don't know is what you haven't learned
Declare `EnvironmentKey` and read `safeAreaInsets` from key window in `connectedScenes` ```swift struct SafeAreaInsetsKey: EnvironmentKey { static var defaultValue: EdgeInsets { UIApplication.shared.keyWindow?.safeAreaInsets.swiftUIInsets ?? EdgeInsets() } } private extension UIEdgeInsets { var...
Declare generic on `RawRepresentable` ```swift import SwiftUI struct TabStrip: View where T.RawValue == String { let values: [T] @Binding var selected: T var body: some View { ScrollView(.horizontal, showsIndicators: false)...
One seemingly obvious way to use ForEach on array with indices is using `enumerated` ```swift struct Book: Hashable, Identifiable { let id: UUID let name: String } struct BooksView: View...
Used to replace credit card regex `30[0-5]#-######-###L` in [EasyFake](https://github.com/onmyway133/EasyFake) We can use `?` to have non-greedy behavior, or I here use square bracket to fine-tune the expression `\{[\d*,]*\d*\}` Also, I...
Use locales data from `faker.js` to https://github.com/onmyway133/EasyFake, renaming files since files, regardless of sub directories in Xcode, must have different name. We use `enumerator` API on `FileManager` to traverse all...
I usually use `GeometryReader` in `background` to get size of view, and encapsulate it inside a `ViewModifier` ```swift struct GetHeightModifier: ViewModifier { @Binding var height: CGFloat func body(content: Content) ->...
If we have a Picker inside a View that has DragGesture, chances are when we scroll the wheel, the DragGesture is detected too To prevent this, we can attach a...
In iOS 15, we can use `UISheetPresentationController` to show bottom sheet like native Maps app. But before that there's no such built in bottom sheet in UIKit or SwiftUI. We...
I usually define `ButtonStyle` to encapsulate common styles related to buttons. Here we specify ` .frame(maxWidth: .infinity)` to let this button take the whole width as possible ```swift struct MyActionButtonStyle:...