Using presentViewController from UIView

后端 未结 5 480
鱼传尺愫
鱼传尺愫 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:53

    Here's a more idiomatic Swift 3 version of Shamsudheen's answer:

    extension UIApplication {
    
        static func topViewController() -> UIViewController? {
            guard var top = shared.keyWindow?.rootViewController else {
                return nil
            }
            while let next = top.presentedViewController {
                top = next
            }
            return top
        } 
    }
    

    Then you can just call:

    UIApplication.topViewController()?.present(...)
    

提交回复
热议问题