4

I have the following simple case:

A UINavigationController, with a root view controller which contains a tableView spanning the whole view, and then a button at the bottom of the screen. I create the button like that:

let button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 50, width: self.view.frame.width, height: 50))
self.view.addSubview(button)

This works fine, until I do the following:

navigationController?.navigationBar.isTranslucent = false

This will make my button created disappear. Can someone explain to me the reason for that? And how to fix that? Here is a screenshot of my simple case. The button at the bottom disappears.

Edit: For context: I am using Eureka library for setting up my forms in the tableView, and wanted to add a sticky button at the bottom.

enter image description here

Guy Daher
  • 5,526
  • 5
  • 42
  • 67
  • best way to test use some more value like CGRect(x: 0, y: self.view.frame.height - **150**, width: self.view.frame.width, height: 50)) so you might be get an idea what happens it is – Nitin Gohel Feb 17 '17 at 11:11
  • @NitinGohel Thanks! The button did indeed appear :) I checked the documentation if the `isTranslucent` does something to the layout, but there is nothing mentioned there. Do you have any clue why my button is getting shifted? – Guy Daher Feb 17 '17 at 11:15
  • 1
    see if we use isTranslucent = true then the view calculate y position from top under the navigation bar as a 0. and if we make it false then self.view's Y position calculated from navigation end after 55 px left. so that's why that shifted. and i suggest to do add button from storyboard with bottom constraint that will easy to take care about it instead of add programmatically. – Nitin Gohel Feb 17 '17 at 11:17

1 Answers1

0

Try setting up the automaticallyAdjustsScrollViewInsets property of your UIViewController to false.

You can also take a look at this answer.

Community
  • 1
  • 1
Stefan Stefanov
  • 829
  • 7
  • 23
  • I just edited my answer with a link that might be helpful for you. – Stefan Stefanov Feb 17 '17 at 10:25
  • @StefanStefanov automaticallyAdjustsScrollViewInsets its related to UIScollView and subclass of Scrollview. UIviewController is not a subclass of UIScrollview so that wont effect at all. if in side UIViewController User used UIScrollview, UITableview, UICollectionView or any other object that subclass of UIScrollview then that will helps – Nitin Gohel Feb 17 '17 at 11:12
  • Okay, first of all why does your `UITableView` have the frame of your whole screen since you want a `UIButton` underneath it? Maybe this is causing the problem. You can instantiate your button first and then constrain the tableView.bottom to the button.top. – Stefan Stefanov Feb 17 '17 at 11:29
  • I am using the Eureka library (as mentioned in my Edit), so they are taking care of creating the tableView behind the scene. That's why I got into this situation. But I do have access to the tableView, so I guess I can change the constraints for it, and add the button below it – Guy Daher Feb 17 '17 at 12:47