tableView 上的编程浮动按钮

Programmatical Floating Button over tableView

我正在尝试在 tableViewController 上创建一个浮动按钮,到目前为止我的代码看起来像这样,但是按钮粘在两个单元格之间,所以它不是浮动的...

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)

那是因为 self.view = UITableView inside UITableViewController 所以你需要实现 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
    }

}