0

I want to add the same floating action button in all my View controllers.I have a chat option(which opens up on clicking this button) and that should be accessible from all the pages of the app.How can I do the same.Instead of adding in redundantly in all the View controllers.

Code samples would be very helpful.

Thanks

Bryan
  • 15
  • 1
  • 7

2 Answers2

1

let's add one "SuperviewController" which is derived from UIViewController and then extend all other view controllers from SuperViewController and add UIButton programmatically in SuperViewController, So you can access button in whole app.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
0

You can take Button on window

var Floatbtn = UIButton(type: .custom)

func setFloatingButton(){
    Floatbtn.frame = CGRect(x: 250, y: 450, width: 50, height: 50)
    Floatbtn.setTitle("All Defects", for: .normal)
    Floatbtn.backgroundColor = UIColor(red: 40.0/255.0, green: 167.0/255.0, blue: 128.0/255.0, alpha: 1.0)
    Floatbtn.clipsToBounds = true
    Floatbtn.layer.cornerRadius = 25
    Floatbtn.addTarget(self,action: #selector(OnClockButtonEvent), for: UIControlEvent.touchUpInside)
    if let window = UIApplication.shared.keyWindow {
        window.addSubview(Floatbtn)
    }
}

Remove FloatBtn when viewcontroller is disappear

func viewWillDisappear(_ animated: Bool) {
Floatbtn.removeFromSuperview() }
B K.
  • 534
  • 5
  • 18