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
in action
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 screenshot:
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
Why Does the Following Code Crash on an iPhone 5 But Not an iPhone 5S
For-In Loop and Type Casting Only for Objects Which Match Type
How to Rotate Sprites Around a Joint
How to Call the More Specific Method of Overloading
Session.Datataskwithurl Completionhandler Never Called
Why Does Int(Float(Int.Max)) Give Me an Error
Iphonex Not Call Prefersstatusbarhidden
Using Uiapplicationdelegateadaptor to Get Callbacks from Userdidacceptcloudkitsharewith Not Working
How to Click a Button Programmatically
Swift Test Give Error "Undefined Symbols for Architecture X86_64"
Libsqlite3.Dylib and Libz.Dylib Missing in Xcode 7. How to Use Parse
Swiftui - Add Border to One Edge of an Image
Adding Swift 3 Packages to Xcode 8 Using the Swift Package Manager
How to Find Max Value for Double and Float in Swift
Xcode 11 Beta 3, Build Error "Unknown Attribute 'State'", "Use of Undeclared Type 'View'" etc