RootViewController
WindowのRootViewControllerをすげ替えるとUIViewControllerを変えられるがWindowにくっつけていたViewも消える。。orz。
以下実験コード
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidAppear(_ animated: Bool) { let v = UIView(frame: self.view.bounds) v.backgroundColor = UIColor.blue let gesture = UITapGestureRecognizer(target: self, action: #selector(buttonPushed(sender:))) v.addGestureRecognizer(gesture) self.view.window!.addSubview(v) } @IBAction func buttonPushed(sender:Any?) { print("buttonPushed") let story = UIStoryboard(name: "Main", bundle: nil) let v2 = story.instantiateViewController(withIdentifier: "v2") self.view.window?.rootViewController = v2 } }