2

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

enter image description here

Community
  • 1
  • 1
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38

2 Answers2

3

You can use keyWindow and add your button as a subview.

 var window :UIWindow = UIApplication.sharedApplication().keyWindow
 window.addSubview(yourButton)
Kivannc
  • 106
  • 8
  • Yes, I thought about this. But I haven't seen apple recommending it anywhere. I consider it, but is it a good practice? That's my question.. – Akshansh Thakur Jun 17 '16 at 12:25
  • You see that the code for window is marginally better as it is just two lines, compared to my code. But is it going to cause any troubles later (performance or in general is it a good industry practice)? because I am developing this app for a company. – Akshansh Thakur Jun 17 '16 at 12:26
1

Why not just create a Base view class(sub class of UIViewController) and add the floating button on that view. Create rest of your views as sub class of this Base view. One advantage is that you will have control to hide the floating button when you want to instead of removing it from window and adding it again.

Edit enter image description here

mshrestha
  • 788
  • 5
  • 14
  • "Create rest of your views as sub class of this Base view"? i am sorry, I am not able to understand your answer :( – Akshansh Thakur Jun 17 '16 at 10:02
  • I added a screenshot for you to get better understanding. BaseVC can be UIViewController with your floating button as subview in it. Later you can hide or show the floating button as you need on different other views that you create. Hope this helps. – mshrestha Jun 17 '16 at 10:08