Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraints resets when app is going in background - iOS 13

Tags:

I have set a view's leading, trailing constraints normally. I have set its height to static 325. And for bottom constraint I have set 2 constraints 1. with main view's bottom constraint to view's bottom constraint. 2. with main view's bottom constraint to view's top constraint. Now on user's action I just show hide view with animation. So when view gets displayed on the screen and the app goes in background then the view's constraint automatically gets altered and the view gets hidden. This issue is only occurring in iOS 13 devices.

I tried to update its constraints on viewWillAppear() but in iOS 13 the viewWillAppear of ViewControllers is also not called when app is activated from background. Also I don't think , that this is a good idea to update constraints.

Storyboard

Class File

class ViewController: UIViewController {

    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!

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

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.topConstraint.isActive = false
            self.bottomConstraint.isActive = true
            UIView.animate(withDuration: 0.3) {
                self.view.layoutIfNeeded()
            }
        }
    }
}

I don't want my constraints to be changed or updated when app state changes from foreground to background and vice versa.

Please help me with the same.

TIA

like image 993
Rajat Mishra Avatar asked Oct 14 '19 12:10

Rajat Mishra


People also ask

Will iOS terminate the app running in background after a specific time?

At the same time, didReceiveMemoryWarning is invoked for the app. At this point, so that your app continues to run properly, the OS begins terminating apps in the background to free some memory. Once all background apps are terminated, if your app still needs more memory, the OS terminates your app.

Does Apple allow apps to run in background?

Helpful answersThe only apps that are really running in the background are music or navigation apps. Go to Settings>General>Background App Refresh and you can see what other apps are allowed to update data in the background. iOS dynamically manages memory without any user intervention. Thank you!


1 Answers

Also met this issue. Noticed that constraints keep reseting in case if they are not checked Installed in Interface Builder. So, as workaround keep all constraints Installed in IB and change isActive state just in code.

enter image description here

like image 188
brigadir Avatar answered Oct 14 '22 21:10

brigadir