Search code examples
iosswiftuibuttonautolayout

Button that is attached to the particular part of the screen


I want to add a button that is attached to the particular part of the screen. It means that when I scroll my TableView, the position of this button on the screen doesn't change. I want my screen to look exactly like this or this app.

I would be grateful for any help. Thank you!


Solution

  • If you want to add global Button then use.

    class func addGlobalButton() {
    
        let bounds = UIScreen.main.bounds
    
        //Create Button Frame
        let button = UIButton(frame: CGRect(x: bounds.width - 90, y: bounds.height - 150, width: 80, height: 80))
    
        //For Circular button
        button.clipsToBounds = true
        button.layer.cornerRadius = 40
    
        button.backgroundColor = UIColor.red
        button.setTitle("+", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
    
        button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
    
        UIApplication.shared.keyWindow?.addSubview(button)
    }
    
    class func buttonClicked() {
      print("Button Clicked")
    }
    

    Put this function in app delegate and call once in any view controller as :

    AppDelegate.addGlobalButton()