GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Menu dismissal problem - depends on menu content

Forums > SwiftUI

My app needs to track the user's location but each time my locationManager sends a location update it dismisses a toolbar menu (if it is displayed). The odd thing is that if the menu buttons have empty actions or just a print statement then all is fine but as soon as a method is put in there it dismisses upon the next UI update.

iOS: 17.0

XCode: Version 15.0 beta 3

Background Modes: Location updates

Privacy - Location When In Use Usage Description = You are being watched!

import SwiftUI

@main
struct DetectHelpApp: App {

    var locationManager = LocationManager()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(locationManager)
        }
    }
}
import MapKit
import SwiftUI

struct ContentView: View {

    @Environment(LocationManager.self) var locationManager

    @State private var cameraPosition: MapCameraPosition = .region(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.556000, longitude: -0.279511), latitudinalMeters: 1000, longitudinalMeters: 1000))

    var body: some View {
        NavigationStack {
            Map(position: $cameraPosition) {
                UserAnnotation()
            }
            .onChange(of: locationManager.lastLocation) { oldValue, newValue in
                print(locationManager.lastLocation)
                withAnimation() {
                    // If the centre of the map is locked to the user's location, then recompose with the new coordinate.
                    cameraPosition = .region(MKCoordinateRegion(center: newValue.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000))
                }
            } // onChange - end
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Menu("Menu") {
                        Button("Start") { help() /*print("Start")*/ }
                        Button("Resume") { print("Resume") }
                    }
                }
            }
            .navigationTitle("Scooby Doo")
        }
    }

    func help() { }
}
import CoreLocation
import Observation

@Observable
final class LocationManager: NSObject, CLLocationManagerDelegate {

    var lastLocation: CLLocation = CLLocation(latitude: 51.556000, longitude: -0.279511)

    let locationManager = CLLocationManager()

    init(dog: String = "") {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 2
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // Ensure we have an actual location in the update.
        guard let location = locations.last else { return }

        lastLocation = location // Our published CLLocation.
    }
}

2      

I just couldn't solve this so I've dropped the Menu and decided to go with a Popup which looks good on both iPad and iPhone and lets me flex my tiny design muscles.

2      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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.