Post

Replies

Boosts

Views

Activity

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 SwiftUI 4, IOS 16
I don't understand what the problem is here. You have a native SwiftUI TabView that has its own tab bar (which I think you want to hide), and then a custom tab bar that overlays the system one. It seems like the more tab, that appears when there are more than five tab items, is affecting you're custom tab bar approach. Is this correct? Also, why do you have a system tab bar if you're not going to use it? Just use an if or switch statement, without the TabView, to change between the tabbed views based on what the user has selected from the custom tab bar.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to SwiftUI 4, IOS 16
Thanks for clearing things up. I have found that the previously mentioned modifier should be applied to each view inside of the TabView for it to take effect, like this: TabView { Tab1() .toolbar(.hidden, for: .tabBar) Tab2() .toolbar(.hidden, for: .tabBar) Tab3() .toolbar(.hidden, for: .tabBar) ... } Hopefully that should work. However, I believe there is still the problem of the "More" tab that arises from the TabView's five tab limit. When selecting one of these tabs from the custom tab bar, the view is shown, unexpectedly to the user, in the "More" navigation stack, which isn't pleasant to use. I don't think there is a way to remove this feature, so there are probably only two solutions to this: Only have a maximum of five tabs, or Use the custom tab bar approach without the TabView.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to New ShareLink with custom type is not working
is there any way to deal with this optional? What error are you getting exactly? Is it a simple "Unexpectedly found nil while unwrapping an Optional value" sort of thing? You will need to check that the value in the url property is a genuine URL. You could provide a default URL, maybe this: URL(fileURLWithPath: "/dev/null") but it's up to you and how you manage that url property. ‎ ‎ I must associate destination to Recording.url In the importing closure you would need to return an instance of Recording, given the file URL (received.file). What the example it doing is copying the received file to a location in the user's file system and using that new URL to create a Video object. If you look at the Copy Code snippets for that session, you can see the contents of the Video.copyVideoFile method. So yes, the received/copied destination file URL needs to be assigned to the Recording.url property. ‎ ‎If there are additional issues, please create a new post as this one is about a different (solved?) problem. Don't forget to close this thread.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to Swiftui MacOS App Example Project OnDelete func
Update I have just created a new macOS project with Core Data enabled and run the program without modifying the template code. Even though the onDelete(perform:) modifier has been attached to the ForEach, the delete swipe action won't work as the List is in a sidebar-style state. If you change the list style to something different, like .inset, the swipe action will work. You could use the onDeleteCommand(perform:) modifier where you can delete the currently selected item in the List when the delete key is pressed. You could also add a contextMenu(menuItems:) modifier and add your own Button that deletes the selected item.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to SwiftUI: List selection of custom struct
Because you're using a List with selection enabled, the whole row will be selected/highlighted – that's the default behaviour. What do you mean by only the title text be selectable? Do you only want the title text to respond to taps, or do you only want the title text to be highlighted when selected?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22