Hope all is well. Directly to the issue:
Environment : Version 11.6 / Swift 5
There is a basic view controller(TestViewController) embedded in UINavigationController. Say that this TestViewController has a button that's moving the self.view up by tapping. With the view moved up then leave the app(leaving it inactive or background whatever) and comeback(active again), the moved view is going back to y position 0, which means the view frame is going back to the CGRect(0, 0, yourViewWidth, yourViewHeight).
The repro is code only and no need for Storyboard.
First is a SceneDelegate that is initialising the navigation controller when launching, and printing the calling message in the console when app becomes active. Why I am thinking that it is happening with UINavigationController is that it is not with just UIViewController. You can test it out with commenting/uncommenting the initialising lines for the root view controller in the delegates:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.makeKeyAndVisible()
self.window = window
window.rootViewController = UINavigationController(rootViewController: TestViewController())
/// Please try with normal view controller as below
// let controller = TestViewController()
// window.rootViewController = controller
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("sceneDidBecomeActive")
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
}
Second and last part is even simpler. Have a look:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100))
button.setTitle("click", for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
button.backgroundColor = .green
view.addSubview(button)
}
@objc func buttonTapped() {
view.frame.origin.y -= 200
}
}
gifs.
Can anyone figure out why this is occurring? It might not be due to UINavigationController, so please teach me! Might it is a bug, or intended. No idea at all. Any advice will be highly appreciated!! Many thanks in advance:)