Create a floating button that will came into entire app iOS swift

Solution:

Create a floating button that will show in entire app and having to add an action at the particular viewcontroller.In appdelegate add the code in the declaration part.


    var floatingBtn: UIButton!

Add the functions in the appdelegate.

    func createInfoButton() {
        floatingBtn = UIButton(type: .custom)
floatingBtn.backgroundColor = .black // color put your neadable color
        floatingBtn.layer.cornerRadius = 20
        floatingBtn.layer.borderWidth = 3
        floatingBtn.backgroundColor = UIColor.white
        floatingBtn.layer.borderColor = color.cgColor
        floatingBtn.addTarget(self, action: #selector(showView(sender:)), for: .touchUpInside)
    }
   
        //    MARK: - Add Floating Button
    func floatingButton(yPos:CGFloat,controller:UIViewController,tag:Int,isHidden:Bool) {
        floatingBtn.frame = CGRect(x: controller.view.frame.size.width-55, y: yPos, width: 40, height: 40)
        floatingBtn.tag = tag
        floatingBtn.isHidden = !isHidden
        if !(floatingBtn.isDescendant(of: controller.view)) {
            controller.view.addSubview(floatingBtn)
        }
        else{
            if !isHidden{
                floatingBtn.removeFromSuperview()
            }
        }
       // print("count of views",controller.view.subviews)
        controller.view.bringSubview(toFront: floatingBtn)
    }

         //    MARK: -  Floating Buttonaction
    func showView(sender:UIButton) {
}





In didfinish Launching call the createinfo function

        createInfoButton()

In your particularviewcontroller call the floatingButton function and add it to your particular viewcontroller in the particular y position.If you want the x position also then pass it to the function and customize it.

Your call in the viewcontroller be like below

        appdel.floatingButton(yPos: 200 , controller: self,tag:1, isHidden: false, color: .white)



   
  

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift