Add button on top of UITableViewController (Swift)

前端 未结 8 2013
盖世英雄少女心
盖世英雄少女心 2021-02-07 16:02

I am trying to add a button ontop of a uitableview controller table view. The view controller has a navigation controller and static cells, which is why it is a uitableviewcontr

8条回答
  •  清歌不尽
    2021-02-07 16:06

    there is a better solution for this. you can do this by disabling the Auto Layout(button.translatesAutoresizingMaskIntoConstraints = false) property of the corresponding Button or any UIView for floating button:

    Swift 4

    //create a button or any UIView and add to subview
    let button=UIButton.init(type: .system)
    button.setTitle("NEXT", for: .normal)
    button.frame.size = CGSize(width: 100, height: 50)
    self.view.addSubview(button)
    
    //set constrains
    button.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
         button.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
         button.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
         button.rightAnchor.constraint(equalTo: tableView.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
         button.bottomAnchor.constraint(equalTo: tableView.layoutMarginsGuide.bottomAnchor, constant: -10).isActive = true
    }
    

提交回复
热议问题