-1

I'm trying to create a floating button over tableViewController, so far my code looks like this, but the Button sticks between two cells, so it's not floating...

let button = UIButton(frame: CGRect(x: 150, y: 550, width: 75, height: 75))
button.backgroundColor = .yellow
button.setTitle("To Jobs", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
J. Doe
  • 285
  • 4
  • 13
  • You could add the button to the window, so it will be on top of the tableView. – Milander Aug 07 '18 at 11:38
  • What do you mean by " Button sticks between two cells, so it's not floating..." ?? – Anuraj Aug 07 '18 at 11:40
  • Are you saying that the button moves when the table view is scrolled ? – Anuraj Aug 07 '18 at 11:41
  • Refer to this question https://stackoverflow.com/questions/48298984/insert-a-floating-action-button-on-uitableview-in-swift-4 it might help you some way – Saurabh Aug 07 '18 at 12:18

1 Answers1

3

That's because self.view = UITableView inside UITableViewController , so you need to implement scrollViewDidScroll

class TableViewController: UITableViewController {

    let button = UIButton(frame: CGRect(x: 150, y: 550, width: 75, height: 75))

    override func viewDidLoad() {
        super.viewDidLoad()

        button.backgroundColor = .yellow
        button.setTitle("To Jobs", for: .normal)
        self.view.addSubview(button)

    }

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

        button.frame.origin.y = 550 + scrollView.contentOffset.y
    }

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87