Swift: Floating Plus Button Over Tableview Using the Storyboard

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

How should I make Floating Button only exist specific ViewController?

For Particular View Controller :

Hey You just need to drag button on tableview using xib or storyboard for specific view controller nothing else i have attached below screenshotSample Image:

For Through out the App :

Or if you want to add floating button through out the app then use this link to implement floating button :

In iOS, how do I create a button that is always on top of all other view controllers?

Swift 3 - Floating Button over UICollectionView

// update this code

override func viewDidLoad() {
super.viewDidLoad()

self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)

self.loadAPI(Page: 1)
}


Related Topics



Leave a reply



Submit