User Location Focus SwiftUI - swiftui

I'm trying to program a ToDo app and want to design the whole thing as a map on which the individual points on the ToDo list are displayed as pins and an event should be carried out every time one of these pins is reached. To test the whole thing, I took a GPS track that is supposed to simulate a bus ride and feeds my app with GPS data. The individual points are on this GPS track.
My app has a button that, when you press it, always focuses on the user location. The problem is he always focuses on the starting point of the GPS track and not on the current location of the GPS track.
Would be very grateful for help!
This is the focus function and the location update function
#Published var region : MKCoordinateRegion!
func focusLocation() {
guard let _ = region else{return}
mapView.setRegion(region, animated: true)
mapView.setVisibleMapRect(mapView.visibleMapRect, animated: true)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else{return}
self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 10000,
longitudinalMeters: 10000)
self.mapView.setRegion(self.region, animated: true)
self.mapView.setVisibleMapRect(self.mapView.visibleMapRect, animated: true)
}

you could try something like this when you press the button, given you have the coordinates to focus on:
func focusLocation(gotoLocation: CLLocationCoordinate2D) {
let rangeInMeters: Double = 10000 // <-- adjust as desired
let coordinateRegion = MKCoordinateRegion.init(center: gotoLocation,
latitudinalMeters: rangeInMeters,
longitudinalMeters: rangeInMeters)
mapView.setRegion(coordinateRegion, animated: true)
}

Related

How to take photo automatically by handling the shutter button

understand iphone has the auto taking photo feature by setting the timer. after this press the shutter button to start.
I want to do something similar. So How to handle the Shutter Button programmatically after the camera has launched.
- implement: UINavigationControllerDelegate, UIImagePickerControllerDelegate
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
}
//-- launch the camera
#IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
So, after the camera launch, how to programmatically handle the Shutter button like clicking it to take photo?
So, after the camera launch, how to programmatically handle the Shutter button like clicking it to take photo?
Tell the UIImagePickerController to takePicture.

Snapshotting a view that has not been rendered results in an empty snapshot when view presented modally - Swift 3

I have a Table View Controller as a tab. In it, there's a button that takes you to a Facebook profile:
func didTapFacebook() {
let url = URL(string: "http://www.facebook.com/" + myFacebookId)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
Works fine every time you press the button.
From another tab, there's a button that presents modally a navigation controller with Table View Controller as its root:
func segueToTable(_ sender: UIViewController, tvc: MyTableViewController, completion: #escaping ((_ done: Bool) -> Void)) {
let nc = MyNavigationController(rootViewController: tvc)
sender.present(nc, animated: true, completion: {
completion(true)
})
}
…
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tvc = storyboard.instantiateViewController(withIdentifier: "TVC") as! MyTableViewController
segueToTable(self, tvc: tvc, completion: { done in
print(“segue complete”)
})
Now once you tap the same button to go to Facebook profile (or Twitter button, basically anything that causes the app to go to background), this warning occurs (there is no keyboard displayed on the screen at this time):
Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
Returning to the app and pressing the button again causes this warning to appear (and every attempt after that):
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
I realize this is related to snapshotting a view that automatically occurs prior to entering the background. It appears for some reason it doesn't like doing that within a view that was presented modally. I've tried a variety of things based on other posts but cannot get the warnings to go away.
Any help is appreciated.

How to change the way in which the root controller of a navigation controller is appearing on screen?

The navigation controller is not the initial view controller. It is being instantiated in the initial VC and supplied with a root controller, the second VC. When I call present view controller function I cannot change the way in which this new VC in being presented, it always appears from the bottom. I did not find how to make it come from right to left. I tried to change who gets the call to modalPresentaionStyle from the second VC to the nav con and back but no change.
Code listing below. Thank you for your help.
#objc fileprivate func showRegisterScreen() {
let registerAccountVC = RegisterAccountVC()
let navigationController = UINavigationController(rootViewController: registerAccountVC)
navigationController.modalPresentationStyle = .pageSheet
present(navigationController, animated: true, completion: nil)
}
This will add a custom animation from right to left.
let transition = CATransition.init()
transition.duration = 0.4
transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window?.layer.add(transition, forKey: nil)
present(navigationController, animated: false, completion: nil)

Automatic presenting View Controller

I have 2 view controllers on my storyboard: viewController1(it input point) and viewController2. After loading the application i want to automatic presenting viewController2 after viewController1.
How i can do that?
if you want to go to second viewcontroller you can add this code to you viewDidLoad method:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier") as! secondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
remeber to add a identifier for the second viewcontroller in storyboard and chenge the identifier that you use with "yourIdentifier"
if you don't want a animation put the animated: true to false.
if you dont want to show the fist viewcontroller, go into you storyboard and click on the second viewcontroller, then in the right side on attributes inspector select the box int the image:
For change viewcontroller you have to embed the first viewcontroller with a navigation controller, if you don't know how to do it, just select the fisrt viewcontroller and do like the image, click in navigation controller
Have you tried to show it in func viewDidLoad() {} like:
override func viewDidLoad() {
// ...
self.present(viewController, animated: true, completion: nil)
}
If you want to straight up load a different view controller you can do this in your app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "put your identifier here")
self.window?.rootViewController = vc
return true
}

SearchBar of UISearchController is lying under statusbar when it actives( ios 9.3 ,ios 8.4)

let storyboard = UIStoryboard(name: "ProductSearch_iPhone", bundle: nil)
let controller : RSearchResultController = storyboard.instantiateViewController(withIdentifier: "SResult")
as! RSearchResultController
searchController = UISearchController(searchResultsController:controller)
searchController.searchResultsUpdater = controller
searchController.dimsBackgroundDuringPresentation = true
controller.delegate = self
searchController.searchBar.delegate = controller
self.present(searchController, animated: true, completion: nil)
definesPresentationContext = true
I've added UISearchController by not integrated with UITableview like many Other tutorials.
Finally i found the solution.The issue was not device specific, ios 9 and ios8 shows wrecked design while in ios10 it is fine.
definesPresentationContext = true
The above line of code was the issue, which was written on the intention searchbar should not be there when i navigate to somewhere & comeback while UISearchController is active. But according to my requirement i have no choice of navigation when UISearchController is active.