|
| 1 | +// |
| 2 | +// View.swift |
| 3 | +// iOS-Common-Libraries |
| 4 | +// |
| 5 | +// Created by Dinesh Harjani on 9/8/22. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +// MARK: - FormIniOSListInMacOS |
| 11 | + |
| 12 | +#if os(iOS) |
| 13 | +typealias FormIniOSListInMacOS = Form |
| 14 | +#elseif os(macOS) |
| 15 | +typealias FormIniOSListInMacOS = List |
| 16 | +#endif |
| 17 | + |
| 18 | +// MARK: - View |
| 19 | + |
| 20 | +extension View { |
| 21 | + |
| 22 | + @inlinable public func frame(size: CGSize, alignment: Alignment = .center) -> some View { |
| 23 | + return frame(width: size.width, height: size.height, alignment: alignment) |
| 24 | + } |
| 25 | + |
| 26 | + @inlinable public func centerTextInsideForm() -> some View { |
| 27 | + // Hack. |
| 28 | + return frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center) |
| 29 | + } |
| 30 | + |
| 31 | + @inlinable public func withoutListRowInsets() -> some View { |
| 32 | + return listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0)) |
| 33 | + } |
| 34 | + |
| 35 | + // MARK: - NavBar |
| 36 | + |
| 37 | + func setTitle(_ title: String) -> some View { |
| 38 | + #if os(iOS) |
| 39 | + return navigationBarTitle(title, displayMode: .inline) |
| 40 | + #else |
| 41 | + return navigationTitle(title) |
| 42 | + #endif |
| 43 | + } |
| 44 | + |
| 45 | + // MARK: - NavigationView |
| 46 | + |
| 47 | + @ViewBuilder |
| 48 | + func wrapInNavigationViewForiOS() -> some View { |
| 49 | + #if os(iOS) |
| 50 | + NavigationView { |
| 51 | + self |
| 52 | + } |
| 53 | + .setupNavBarBackground() |
| 54 | + .setSingleColumnNavigationViewStyle() |
| 55 | + .accentColor(.white) |
| 56 | + #else |
| 57 | + self |
| 58 | + #endif |
| 59 | + } |
| 60 | + |
| 61 | + // MARK: - UITextField |
| 62 | + |
| 63 | + func disableAllAutocorrections() -> some View { |
| 64 | + disableAutocorrection(true) |
| 65 | + #if os(iOS) |
| 66 | + .autocapitalization(.none) |
| 67 | + #endif |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// MARK: - Picker |
| 72 | + |
| 73 | +extension Picker { |
| 74 | + |
| 75 | + @ViewBuilder |
| 76 | + func setAsComboBoxStyle() -> some View { |
| 77 | + self |
| 78 | + #if os(iOS) |
| 79 | + .pickerStyle(MenuPickerStyle()) |
| 80 | + .accentColor(.primary) |
| 81 | + #endif |
| 82 | + } |
| 83 | + |
| 84 | + func setAsSegmentedControlStyle() -> some View { |
| 85 | + self |
| 86 | + #if os(iOS) |
| 87 | + .pickerStyle(SegmentedPickerStyle()) |
| 88 | + #endif |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// MARK: - NavigationView |
| 93 | + |
| 94 | +extension NavigationView { |
| 95 | + |
| 96 | + func setSingleColumnNavigationViewStyle() -> some View { |
| 97 | + self |
| 98 | + #if os(iOS) |
| 99 | + .navigationViewStyle(StackNavigationViewStyle()) |
| 100 | + #endif |
| 101 | + } |
| 102 | + |
| 103 | + func setupNavBarBackground() -> NavigationView { |
| 104 | + #if os(iOS) |
| 105 | + let appearance = UINavigationBarAppearance() |
| 106 | + let attributes: [NSAttributedString.Key: Any] = [ |
| 107 | + .foregroundColor: UIColor.white |
| 108 | + ] |
| 109 | + appearance.titleTextAttributes = attributes |
| 110 | + appearance.largeTitleTextAttributes = attributes |
| 111 | + appearance.backgroundColor = Assets.navBarBackground.uiColor // Dynamic Color. |
| 112 | + UINavigationBar.appearance().compactAppearance = appearance |
| 113 | + UINavigationBar.appearance().standardAppearance = appearance |
| 114 | + UINavigationBar.appearance().scrollEdgeAppearance = appearance |
| 115 | + #endif |
| 116 | + return self |
| 117 | + } |
| 118 | +} |
0 commit comments