In iOS, how do I create a button that is always on top of all other view controllers?

前端 未结 8 1885
悲哀的现实
悲哀的现实 2020-11-28 02:09

No matter if modals are presented or the user performs any type of segue.

Is there a way to keep the button \"always on top\" (not the top of the screen) throughout

8条回答
  •  野性不改
    2020-11-28 02:46

    Present it on this:

    public static var topMostVC: UIViewController? {
        var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
        while let pVC = presentedVC?.presentedViewController {
            presentedVC = pVC
        }
    
        if presentedVC == nil {
            print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
        }
        return presentedVC
    }
    
    topMostVC?.presentViewController(myAlertController, animated: true, completion: nil)
    
    //or
    
    let myView = UIView()
    topMostVC?.view?.addSubview(myView)
    

    These are included as a standard function in a project of mine: https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题