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

前端 未结 8 1833
悲哀的现实
悲哀的现实 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:41

    You can use the zPosition property of the view's layer (it's a CALayer object) to change the z-index of the view, also you need to add this button or your view as subview of your window not as subview of your any other viewcontrollers, its default value is 0. But you can change it like this:

    yourButton.layer.zPosition = 1;
    

    You need to import the QuartzCore framework to access the layer. Just add this line of code at the top of your implementation file.

    #import "QuartzCore/QuartzCore.h"
    

    Hope this helps.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题