TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

WatchOS SwiftUI Hide button under navigation bar instead of a context menu

Forums > SwiftUI

Hello,

Since the hard press context menus are now depreciated in WatchOS 7. I would like to hide a sort list button under the navigation bar of the watch screen. My screen is currently just a simple Carousel list and id like to be able to sort the list by either date or name by setting a user default and then reload the list.

Sort of like this ->


if default = 0 {
  sortedUsers = users.sorted(by: { $0.date! < $1.date! })
} else if default = 1 {
  sortedUsers = users.sorted(by: { $0.name! < $1.name! })
 }

//

NavigationView {
                List {
                    Button {
                        if UserDefaults.standard.integer(forKey: "sortBy") == 0 {
                            UserDefaults.standard.setValue(1, forKey: "sortBy")
                            sortTable()
                        } else {
                            UserDefaults.standard.setValue(0, forKey: "sortBy")
                            sortTable()
                        }
                    } label: {
                        HStack {
                            if UserDefaults.standard.integer(forKey: "sortBy") == 0 {
                                Spacer()
                                Image(systemName: "calendar.circle")
                                Text("Date order")
                                Spacer()
                            } else {
                                Spacer()
                                Image(systemName: "person.circle")
                                Text("Name order")
                                Spacer()
                            }
                        }
                    }
                    ForEach(sortedUsers, id: \.contactID) {
                        UserRow(user: $0)
                    }
                }
            }
            .navigationBarTitle("APP NAME")
            .navigationBarBackButtonHidden(true)
            .listStyle(CarouselListStyle())

Currently the button appears at the top of the list and its working to reload the list. i used @state for the array called sortedUsers. I was wondering how to hide the button at first. Also when pulling down make the button detatch slightly from the list and stay fixed to the top of the screen like in the mail app. Thanks.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.