swiftuiviewsafearealayoutguideios-animationsuisearchresultscontroller

Animations with safeAreaLayoutGuide (Swift 5)


I would like a smooth animation of this view whenever the search bar is selected and deselected. Right now it's choppy:

enter image description here

Heres my code below in the searchResultsUpdater. From what I understand, these functions should handle the animations, I'm not sure what's wrong here:

func updateSearchResults(for searchController: UISearchController) {
    
    //MapView moves up when search bar is selected
    if searchController.isActive == true{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y

        },completion: nil)
    }
    
    //MapView moves down when cancel button is selected
    if searchController.isActive == false{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y
            
        },completion: nil)
    }
}

Any help is appreciated, thank you!


Solution

  • I found the solution. Originally I had the CGRect for the mapView frame in viewDidLayoutSubviews():

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
         //layout to fit frame of view
        mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
        view.frame.size.width, height: view.frame.size.height - 
        view.safeAreaInsets.top)
      
    }
    

    I moved it to viewDidLoad(), and now it works:

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //add gesture recognizer for mapView
        mapView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(mapViewAnimation)))
        //layout to fit frame of view
        mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
        view.frame.size.width, height: view.frame.size.height - 
        view.safeAreaInsets.top)
    
    }
    

    I also added a gesture recognizer instead so that the searchResultsUpdater wouldn't be so cluttered:

    //handle the animation for mapview
    @objc func mapViewAnimation(){
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: 
        UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            
        self.mapView.frame.origin.y = 
        self.view.safeAreaLayoutGuide.layoutFrame.origin.y
            
        },completion: nil)
        
    }
    

    enter image description here

    If anyone knows why it didn't work when I had the CGRect in didLayoutSubViews() feel free to let me know. Thanks!