So I have been working on adding a button on to the screen, where it doesn't move at all (even with scrollView/ Table View).
So, basically a floating action button like in android.
I have used the following libraries, they work fine but have bugs and aren't optimal :
- KCFloatingActionButton
- LiquidFloatingActionButton
So I decided to create my own button. Now the issue is that I don't know where to add the button. I am using a tab bar controller
I have seen this question.
it proposes following solutions:
1) Use UIViewController instead of UITableVC, but it is not possible for me to do that.
2) The second solution doesn't work since the addition of auto-layout.
3) Another is to add button on navigation Controller if i have a navigation controller.
I am personally considering adding it on UIWindow. But I don't know if I should do that. What are the suggestions and best practices for adding a floating action button?
This is what I have tried :
let fab : UIButton = UIButton()
fab.translatesAutoresizingMaskIntoConstraints = false
fab.center.x = (self.navigationController?.view.center.x)! + (self.navigationController?.view.frame.width)!/2 - 50.0
fab.center.y = (self.navigationController?.view.center.y)! + (self.navigationController?.view.frame.height)!/2 - 110.0
fab.frame.size = CGSize(width: 60.0, height: 60.0)
self.navigationController?.view.addSubview(fab)
var constraintSet = [NSLayoutConstraint]()
constraintSet.append(fab.centerXAnchor.constraintEqualToAnchor(self.navigationController?.view.centerXAnchor, constant: self.view.frame.width/2 - 50.0))
constraintSet.append(fab.centerYAnchor.constraintEqualToAnchor(self.navigationController?.view.centerYAnchor, constant: self.view.frame.height/2 - 110.0))
constraintSet.append(fab.heightAnchor.constraintEqualToConstant(60.0))
constraintSet.append(fab.widthAnchor.constraintEqualToConstant(60.0))
self.navigationController?.view.addConstraints(constraintSet)
fab.layer.zPosition = 3.0
fab.setImage(UIImage(named: "fab"), forState: .Normal)
Here is fab image : if someone wants to try