4

I want to get the distance of a button to the bottom of the screen in Swift 3.

I can see the correct value in the Storyboard when I look at the distance (alt key) but unfortunately I am not able to calculate it manually.

The value I am looking for is the same as the constant in the "Vertical Spacing to Bottom Layout" constraint for the button.

view.frame.maxY - randomButton.frame.maxY

gave me a value way too high.

Michael
  • 516
  • 1
  • 6
  • 15
  • 1
    Please add a bit more code, like *where* this single line is. Depending on where it is - like viewDidLoad() - you could actually end up with a negative value. – dfd Jan 27 '17 at 17:43
  • What I said it you are actually trying to do? It sounds like you want to place another view in between the button and the bottom of the screen? If so you should be using auto layout or stack views. – Fogmeister Jan 27 '17 at 17:47
  • The code is viewDidLoad – Michael Jan 27 '17 at 17:55
  • 1
    You shouldn't calculate UI related operations in `viewDidLoad`. Move your code in `viewDidLayoutSubvies` or `viewDidAppear` – Rikh Jan 27 '17 at 17:56
  • I tried that but unfortunately I am getting the same results as before – Michael Jan 27 '17 at 18:00
  • @Fogmeister I want to compare the space after the button with the height of the keyboard and want to move the screen up if there is not enough space. Everything works perfect, I am just not able to calculate the empty space... – Michael Jan 27 '17 at 18:04
  • @Michael then you should be using auto layout or stack views. They will do all of this for you. No need to calculate anything. – Fogmeister Jan 27 '17 at 18:07

3 Answers3

9
view.frame.size.height - (button.frame.size.height + button.frame.origin.y)

I think its ok! Hope it helps

Lucas Palaian
  • 344
  • 2
  • 11
9

If your button is not a direct successor of the view controller's view (aka, the hierarchy is something like ViewController's View -> SomeOtherView->Button), you won't get the right math by simply using frames. You will have to translate the button's Y-position to the coordinate space of the window object or your view controller's main view.

Take a look at this question: Convert a UIView origin point to its Window coordinate system

let realOrigin = someView.convert(button.frame.origin, to: self.view)

Then apply the math suggested by Lucas Palaian.

let bottomSpace = view.frame.maxY - (button.frame.height + realOrigin.y)

Another work around, in case something really wild and wierd is going on there, is to drag and drop an outlet of the button's bottom constraint. (Select the constraint from the view hierarchy in Interface Builder, hold the control key and drag the constraint to your view controller.) Then in your code you can access the constant.

let bottomSpace = myButtonBottomConstraint.constant
Community
  • 1
  • 1
0

Use this to have it from the bottom of the button to the bottom of the view (Tested):

view.frame.size.height - randomButton.frame.size.height/2 - randomButton.frame.origin.y
Mago Nicolas Palacios
  • 2,139
  • 12
  • 26