How to Add Floating Button on Top of the Uitableview

Programmatical Floating Button over tableView

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
}

}

Insert a floating action button on UITableView in Swift

If you currently using tableViewController then no , you must subclass UIViewController add UItableView and your floating button to it

Or you may override scollviewDidScroll and change button y according to tableview current offset

drag scrollview as IBOutlet and set it's delegate to the viewController

   func scrollViewDidScroll(_ scrollView: UIScrollView) {

let off = scrollView.contentOffset.y

btn.frame = CGRect(x: 285, y: off + 485, width: btn.frame.size.width, height: btn.frame.size.height)
}

code snapshot

Sample Image

in action

Sample Image
see in action

iOS floating button over table view

Perhaps adding a toolbar with buttons at the bottom would be a better approach?

Floating button over UItableview using storyboards

I think best thing you have to do is to create a UIViewController and add it a UITableView. Then you can add also the UIButton you want to the view controller's view. Don't forget to set the view controller to be the delegate and data source for your table view, and to add <UITableViewDataSource,UITableViewDelegate> to your view controller interface.

How to put buttons over UITableView which won't scroll with table in iOS

One solution to this is to extend UIViewController instead of UITableViewController. You will need to add your own table view and set everything up. Then you can add your button's to the view controller's view instead of the table view.



Related Topics



Leave a reply



Submit