Using presentViewController from UIView

后端 未结 5 470
鱼传尺愫
鱼传尺愫 2020-12-13 14:52

I am creating a title bar for my iOS application and I am making this inside a UIView. The only issue I am having is with the \"home button\". When the home button is presse

5条回答
  •  执笔经年
    2020-12-13 15:33

    With Swift 4 - 4.2 Adding onto Shamsudheen TK's answer.

    var topVC = UIApplication.shared.keyWindow?.rootViewController
        while((topVC!.presentedViewController) != nil) {
            topVC = topVC!.presentedViewController
        }
        let customViewController = CustomViewController()
        topVC?.present(customViewController, animated: true, completion: nil)
    

    With UINavigationController: Here is also an additional feature -> You can pass along a UINavigationController with your customViewController.

     var topVC = UIApplication.shared.keyWindow?.rootViewController
        while((topVC!.presentedViewController) != nil) {
            topVC = topVC!.presentedViewController
        }
        let customViewController = CustomViewController()
        let navController = UINavigationController(rootViewController: CustomViewController)
        topVC?.present(navController, animated: true, completion: nil)
    

提交回复
热议问题