0

I have a simple view controller which is being rendered modally simply by using

viewController.present(myVC, animated: true, completion: nil)

myVC view controller does not have any embedded navigation controller and in viewDidLoad of myVC I am setting view which is supposed to behave like navigation bar view (I can't use navigation bar / navigation controller unfortunately)

Here is how I add the view programmatically

            self.view.addSubview(topView)
            topView.translatesAutoresizingMaskIntoConstraints = false
            topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
            topView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
            topView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true
            topView.heightAnchor.constraint(equalToConstant: 70).isActive = true

I have specified extended edges as top in my viewDidLoad of myVC

self.edgesForExtendedLayout = .top

And the UI looks like

enter image description here

There is a gap between status bar and view added highlighted by yellow border which I am not sure how to fix :(

Please help

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • 1
    [This](https://stackoverflow.com/a/46831423/3687801) or [this](https://stackoverflow.com/a/46318300/3687801) could be helpful when you change `topLayoutGuide`(deprecated) to `safeAreaLayoutGuide`. – nayem Nov 15 '18 at 09:34

1 Answers1

1

You get the gap because you are adding your topView's top constraint to the view's topLayoutGuide.bottom (which sits a bit below the notch). So that is intended behavior.

The cleanest way to get rid of that gap is to embed you view controller in a UINavigationController and use a real navigation bar.

But if you cannot do that you have to get rid of the gap yourself.

I cannot think of an elegant way to get rid of this gap but you could add a negative constant to the constraint that is as high as the gap:

topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: -14).isActive = true

But you would have to make sure that this is only done on devices that have a safeAreaLayoutGuide.topAnchor > 0. (iPhoneX etc.). On all other devices the constant has to be 0.

As I said this is not a very stable or elegant solution but it would work.

BTW If possible you should change self.topLayoutGuide.bottomAnchor (deprecated) to view.safeAreaLayoutGuide.topAnchor.

joern
  • 27,354
  • 7
  • 90
  • 105
  • thanks a lot for the response, Ill try your solution and update you soon, but how did you come up with the value of -14 status bar height is 20 nav bar height is 44 then how did you achieve that -14 is that a random number and should I find it by trial n error? – Sandeep Bhandari Nov 15 '18 at 08:40
  • The gap has nothing to do with the status bar. I found the value by trial and error. As I said: not a very elegant nor stable solution ;-) – joern Nov 15 '18 at 08:42
  • thank you for your effort and time,lemme try your answer :) – Sandeep Bhandari Nov 15 '18 at 08:43
  • Hey I have upvoted your answer, the only reason I haven't accepted your answer is it looks like some kinda hack with that constant value :( Ill keep the question open till tomorrow just to see if I get cleaner solution else Ill accept your answer as it does the trick :) – Sandeep Bhandari Nov 15 '18 at 12:40
  • 1
    Fair enough.I would be interested in a cleaner solution as well ;-) – joern Nov 15 '18 at 13:07
  • I have accepted it as an answer as it works but I did not wanted to use the hack of hard coding -14 so I ended up modifying my VC to embed NavigationController which I was initially trying to avoid :| Thanks for your effort and time appreciated :) – Sandeep Bhandari Nov 20 '18 at 15:08
  • 1
    That's definitely a cleaner and better approach. I'll add that to the answer so it is more visible. – joern Nov 20 '18 at 15:39