iosswiftviewcontroller

Background Image causing animated popViewController delay/lag


I am getting a delay on animation when popping a view controller.

The reason I am getting the delay is because I have a background image.

override func viewDidLoad() {
        super.viewDidLoad()
        
       setBackgroundImage()
        
    }

@IBAction func navigationButton(_ sender: Any) {
        
        print("navigation button presssed")
        self.navigationController?.popViewController(animated: true)
    }

func setBackgroundImage() {

        let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
        backgroundImage.image = UIImage(named: "RewardsBackgroundONLY")
        backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
        self.view.insertSubview(backgroundImage, at: 0)
    }

How do I prevent the delay from happening while setting the background image.

I also tried popping the view controller on the main thread, still doesnt work.


Solution

  • Probably your image is too large and the content mode backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill cause this behavior.

    you have two ways to resolve this problem.

    1. change the UIView.ContentMode value and check if is ok for you. try scaleToFill or scaleAspectFit

    2. resize image to fit on your app.