Post

Replies

Boosts

Views

Activity

Reply to How to pass a Swift Type into an Generic SwiftUI View?
Could you not pass in the actual value to GenericView and remove the generics. Something like this: struct GenericView: View {     var component: any Component     var body: some View {         Text(component.name)     } } struct MainView: View {     var body: some View {         GenericView(component: componentType)     }     private var component: any Component {         // This returns a value that conforms to Component     } } Or would this not be possible because it removes the generics, or you just want the type being passed around instead of the actual value? I don't know if any and generics work the best together. You could think of using some instead of any, but it's whatever suits your needs.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’22
Reply to Use iPhone's Proximity Sensor
You can retrieve the proximityState value from the current UIDevice and detect when it changes. You need to enable proximity monitoring first and I suggest doing that in your main App struct. // in main App struct init() {     UIDevice.current.isProximityMonitoringEnabled = true } struct ContentView: View {     @State private var theNumber = 0     var body: some View {         Text("\(theNumber)")             .onReceive(NotificationCenter.default.publisher(for: UIDevice.proximityStateDidChangeNotification)) { _ in                 if UIDevice.current.proximityState { // sensor is close to user // true is "near", false is "far"                     theNumber += 1                 }             }     } } ‎ Things to Note The sensor detects objects up to 10 cm away. When the sensor detects an object that is close, the screen turns off. There is no single point where the proximity state switches between "near" and "far", but instead a buffer range. When the state is "far", the object must be up to 5 cm from the sensor in order to change the state to "near". When the state is "near", the object must be at least 10 cm from the sensor in order to change the state to "far".
Topic: UI Frameworks SubTopic: UIKit Tags:
Aug ’22
Reply to SwiftUI Mac navigation title is not editable
It works correctly on iOS and iPadOS 16 and that's maybe what the documentation is referring to. In the Renaming section of this article, it explains this feature is available on macOS but the indicator is for iOS. I haven't seen editable navigation titles yet in macOS Ventura, so I don't know what they would look like.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’22
Reply to How to show SwiftUI PhotosPicker programatically
There is already a modifier for this. It has the same parameters as the PhotosPicker view and comes in its different variants. photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:photoLibrary:) -> some View I couldn't find it in the documentation, but you can find it in Xcode's generated interface for SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to implementing form behaviour in custom inputs
iOS 16 You can use the new Grid API with a custom alignment for the labels. Grid(alignment: .leadingFirstTextBaseline) { GridRow { Text("Username:") .gridColumnAlignment(.trailing) // align the entire first column TextField("Enter username", text: $username) } GridRow { Label("Password:", systemImage: "lock.fill") SecureField("Enter password", text: $password) } GridRow { Color.clear .gridCellUnsizedAxes([.vertical, .horizontal]) Toggle("Show password", isOn: $showingPassword) } } ‎ iOS 15 and earlier You can achieve this through the use of custom alignment guides and a custom view that wraps up the functionality for each row. extension HorizontalAlignment { private struct CentredForm: AlignmentID { static func defaultValue(in context: ViewDimensions) -> CGFloat { context[HorizontalAlignment.center] } } static let centredForm = Self(CentredForm.self) } struct Row<Label: View, Content: View> { private let label: Label private let content: Content init(@ViewBuilder content: () -> Content, @ViewBuilder label: () -> Label) { self.label = label self.content = content() } init(@ViewBuilder content: () -> Content) where Label == EmptyView { self.init(content: content) { EmptyView() } } init(_ titleKey: LocalizedStringKey, @ViewBuilder content: () -> Content) where Label == Text { self.init(content: content) { Text(titleKey) } }     init<S: StringProtocol>(_ title: S, @ViewBuilder content: () -> Content) where Label == Text { self.init(content: content) { Text(title) } } var body: some View { HStack { label.alignmentGuide(.centredForm) { $0[.trailing] } content.alignmentGuide(.centredForm) { $0[.leading] } } } } The multiple initialisers are there for convenience and taken from the standard SwiftUI controls. Fell free to remove the ones you don't use. It can then be implemented like this: // need to have the alignment parameter for it to work VStack(alignment: .centredForm) { // with text label Row("Username:") { TextField("Enter username", text: $username) } // with view label Row { SecureField("Enter password", text: $password) } label: { Label("Password:", systemImage: "lock.fill") } // without label but still aligned correctly Row { Toggle("Show password", isOn: $showingPassword) } } ‎ ‎Obviously, place your own views in where they need to go. Both solutions will work, just choose the one you want to use (bearing in mind target version).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to error build: Cannot find 'NavigationStack' in scope
It happens when you develop for iOS, iPadOS, and MacOS. Make sure that your macOS minimum deployment target is set to version 13.0, otherwise NavigationStack will not be available. I'm assuming you're iOS target version is 16.0. ‎ Just installed Ventura beta 7, now two new additional errors.. Double-check you are targeting iOS 16 and/or macOS 13 where NavigationStack, and other new APIs, are available. If not, you will have to provide more information on where these errors are coming from.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to error build: Cannot find 'NavigationStack' in scope
Maybe this will explain things (from the macOS and Xcode release notes): ‎ The macOS 13 SDK provides support to develop apps for Mac computers running macOS 13 Ventura beta 7. The SDK comes bundled with Xcode 14 beta 6 ... Xcode 14 RC includes Swift 5.7 and SDKs for iOS 16, iPadOS 16, tvOS 16, watchOS 9, and macOS Monterey 12.5.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to How do I show the whole month name and not 3 letters?
Pass in .wide as the parameter for the month method, like this: .month(.wide)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to SwiftUI Support
I have filed feedback for this: FB11212680.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to How to pass a Swift Type into an Generic SwiftUI View?
Could you not pass in the actual value to GenericView and remove the generics. Something like this: struct GenericView: View {     var component: any Component     var body: some View {         Text(component.name)     } } struct MainView: View {     var body: some View {         GenericView(component: componentType)     }     private var component: any Component {         // This returns a value that conforms to Component     } } Or would this not be possible because it removes the generics, or you just want the type being passed around instead of the actual value? I don't know if any and generics work the best together. You could think of using some instead of any, but it's whatever suits your needs.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to SwiftUI Refreshing Mechanism
Please file a feedback report. This seems to be a problem seen by a lot of people, and is due to the way SwiftUI handles the Binding in the List.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to Use iPhone's Proximity Sensor
You can retrieve the proximityState value from the current UIDevice and detect when it changes. You need to enable proximity monitoring first and I suggest doing that in your main App struct. // in main App struct init() {     UIDevice.current.isProximityMonitoringEnabled = true } struct ContentView: View {     @State private var theNumber = 0     var body: some View {         Text("\(theNumber)")             .onReceive(NotificationCenter.default.publisher(for: UIDevice.proximityStateDidChangeNotification)) { _ in                 if UIDevice.current.proximityState { // sensor is close to user // true is "near", false is "far"                     theNumber += 1                 }             }     } } ‎ Things to Note The sensor detects objects up to 10 cm away. When the sensor detects an object that is close, the screen turns off. There is no single point where the proximity state switches between "near" and "far", but instead a buffer range. When the state is "far", the object must be up to 5 cm from the sensor in order to change the state to "near". When the state is "near", the object must be at least 10 cm from the sensor in order to change the state to "far".
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to SwiftUI Mac navigation title is not editable
It works correctly on iOS and iPadOS 16 and that's maybe what the documentation is referring to. In the Renaming section of this article, it explains this feature is available on macOS but the indicator is for iOS. I haven't seen editable navigation titles yet in macOS Ventura, so I don't know what they would look like.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to NavigationSplitView on watchOS
NavigationSplitView isn't really suited for watchOS as it can only show one view at a time. It would only become a problem if a companion iOS app needed the split view.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’22
Reply to How to show SwiftUI PhotosPicker programatically
There is already a modifier for this. It has the same parameters as the PhotosPicker view and comes in its different variants. photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:photoLibrary:) -> some View I couldn't find it in the documentation, but you can find it in Xcode's generated interface for SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to Navigation View large distance to the top
Remove the NavigationView from the detail view; you should only have one at the top of the view hierarchy.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to implementing form behaviour in custom inputs
iOS 16 You can use the new Grid API with a custom alignment for the labels. Grid(alignment: .leadingFirstTextBaseline) { GridRow { Text("Username:") .gridColumnAlignment(.trailing) // align the entire first column TextField("Enter username", text: $username) } GridRow { Label("Password:", systemImage: "lock.fill") SecureField("Enter password", text: $password) } GridRow { Color.clear .gridCellUnsizedAxes([.vertical, .horizontal]) Toggle("Show password", isOn: $showingPassword) } } ‎ iOS 15 and earlier You can achieve this through the use of custom alignment guides and a custom view that wraps up the functionality for each row. extension HorizontalAlignment { private struct CentredForm: AlignmentID { static func defaultValue(in context: ViewDimensions) -> CGFloat { context[HorizontalAlignment.center] } } static let centredForm = Self(CentredForm.self) } struct Row<Label: View, Content: View> { private let label: Label private let content: Content init(@ViewBuilder content: () -> Content, @ViewBuilder label: () -> Label) { self.label = label self.content = content() } init(@ViewBuilder content: () -> Content) where Label == EmptyView { self.init(content: content) { EmptyView() } } init(_ titleKey: LocalizedStringKey, @ViewBuilder content: () -> Content) where Label == Text { self.init(content: content) { Text(titleKey) } }     init<S: StringProtocol>(_ title: S, @ViewBuilder content: () -> Content) where Label == Text { self.init(content: content) { Text(title) } } var body: some View { HStack { label.alignmentGuide(.centredForm) { $0[.trailing] } content.alignmentGuide(.centredForm) { $0[.leading] } } } } The multiple initialisers are there for convenience and taken from the standard SwiftUI controls. Fell free to remove the ones you don't use. It can then be implemented like this: // need to have the alignment parameter for it to work VStack(alignment: .centredForm) { // with text label Row("Username:") { TextField("Enter username", text: $username) } // with view label Row { SecureField("Enter password", text: $password) } label: { Label("Password:", systemImage: "lock.fill") } // without label but still aligned correctly Row { Toggle("Show password", isOn: $showingPassword) } } ‎ ‎Obviously, place your own views in where they need to go. Both solutions will work, just choose the one you want to use (bearing in mind target version).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to Accessing Dynamic Island on iPhone 14
See this answer.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to error build: Cannot find 'NavigationStack' in scope
Strange. I am not having this problem on Xcode 14 RC. Are you sure you have imported SwiftUI?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to error build: Cannot find 'NavigationStack' in scope
It happens when you develop for iOS, iPadOS, and MacOS. Make sure that your macOS minimum deployment target is set to version 13.0, otherwise NavigationStack will not be available. I'm assuming you're iOS target version is 16.0. ‎ Just installed Ventura beta 7, now two new additional errors.. Double-check you are targeting iOS 16 and/or macOS 13 where NavigationStack, and other new APIs, are available. If not, you will have to provide more information on where these errors are coming from.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to error build: Cannot find 'NavigationStack' in scope
Maybe this will explain things (from the macOS and Xcode release notes): ‎ The macOS 13 SDK provides support to develop apps for Mac computers running macOS 13 Ventura beta 7. The SDK comes bundled with Xcode 14 beta 6 ... Xcode 14 RC includes Swift 5.7 and SDKs for iOS 16, iPadOS 16, tvOS 16, watchOS 9, and macOS Monterey 12.5.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to SwiftUI 4, IOS 16
If you want to hide the tab bar, you can use the new modifier introduced in iOS 16: toolbar(_:for:). You would use it like this: .toolbar(.hidden, for: .tabBar)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22