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
// Inspired by: https://gist.github.com/dfrib/d7419038f7e680d3f268750d63f0dfae | |
import Foundation | |
public extension Dictionary { | |
subscript(keyPath string: Key, separator: String) -> Value? where Key == String { | |
get { return self[keyPath: string.components(separatedBy: separator)] } | |
set { self[keyPath: string.components(separatedBy: separator)] = newValue } | |
} | |
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
extension Array where Element: Identifiable { | |
public subscript(id: Element.ID) -> Element? { | |
first { $0.id == id } | |
} | |
} | |
// let arrayOfIdentifiables = [] | |
// let itemWithId = arrayOfIdentifiables[id] |
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
extension Spacer { | |
/// https://stackoverflow.com/a/57416760/3393964 | |
public func onTapGesture(count: Int = 1, perform action: @escaping () -> Void) -> some View { | |
ZStack { | |
Color.black.opacity(0.001).onTapGesture(count: count, perform: action) | |
self | |
} | |
} | |
} |
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
import SwiftUI | |
extension View { | |
/// https://stackoverflow.com/a/61985678/3393964 | |
public func cursor(_ cursor: NSCursor) -> some View { | |
self.onHover { inside in | |
if inside { | |
cursor.push() | |
} else { | |
NSCursor.pop() |
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
/// An iOS style TabView that doesn't reset it's childrens navigation stacks when tabs are switched. | |
public struct UIKitTabView: View { | |
private var viewControllers: [UIHostingController<AnyView>] | |
private var selectedIndex: Binding<Int>? | |
@State private var fallbackSelectedIndex: Int = 0 | |
public init(selectedIndex: Binding<Int>? = nil, @TabBuilder _ views: () -> [Tab]) { | |
self.viewControllers = views().map { | |
let host = UIHostingController(rootView: $0.view) | |
host.tabBarItem = $0.barItem |
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
extension Image { | |
/// This helps achieving what `UIView.ContentMode.scaleAspectFit` and `.scaleAspectFill` do in UIImageView.contentMode | |
/// | |
/// The difference between Image.containerAspectRatio (this) and SwiftUIs View.aspectRatio is that the first applies the | |
/// aspect ratio to the view that contains the image, rather than to the image itself. | |
/// | |
/// So in the following example: | |
/// - The first image will scale to a square but the contentMode does not do anything to prevent stretching and wether you use .fit or .fill does not matter. | |
/// - The second image will resize to fit inside a square while maintaining its aspect ratio, similar to how a UIImageView with contentMode set to scaleAspectFit behaves. | |
/// - The third image will resize to fill inside a square while maintaining its aspect ratio similar to how a UIImageView with contentMode set to scaleAspectFill behaves. |
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
import Combine | |
import PublishedObject // https://github.com/Amzd/PublishedObject | |
import SwiftUI | |
/// A property wrapper type that instantiates an observable object. | |
@propertyWrapper | |
public struct StateObject<ObjectType: ObservableObject>: DynamicProperty | |
where ObjectType.ObjectWillChangePublisher == ObservableObjectPublisher { | |
/// Wrapper that helps with initialising without actually having an ObservableObject yet |
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
import SwiftUI | |
@available(iOS 14.0, *) | |
public struct ColorPickerWithoutLabel: UIViewRepresentable { | |
@Binding var selection: Color | |
var supportsAlpha: Bool = true | |
public init(selection: Binding<Color>, supportsAlpha: Bool = true) { | |
self._selection = selection | |
self.supportsAlpha = supportsAlpha |
OlderNewer