10

My view does not have a navigation bar, but I want to display content under status bar. I've checked extend edges under top bars, under opaque bars in my view controller, the view that I want to display under status bar has 0 vertical spacing constraint to top layout guide, but still, here is what I get:

enter image description here

The status bar has 20px solid white background, which I don't want. I want my view to overlap under status bar, just like the mockup below:

enter image description here

How can I do that, without having a visible navigation bar (I still have it as my view is guaranteed to be inside a navigation controller, but it's will never be visible as I have a lot of custom designed sections including top bars)?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Does this mean you have hidden the navigationController's navigationBar? – Edwin Abraham Feb 18 '15 at 15:42
  • @EdAbe yes, I've hidden the navigation bar as I have some heavily customized views that I'm using as top bar/navigation bar. But I need to reach below status bar as well. – Can Poyrazoğlu Feb 18 '15 at 16:14
  • Have you set the extended layouts for the view controller to edge rectzero. For me setting the navigationbar as hidden and the extended layout to zero rect made it work. – Edwin Abraham Feb 18 '15 at 16:23
  • @EdAbe nope, setting `self.edgesForExtendedLayout = UIRectEdgeNone;` (which I assume is the one that you were talking about) did not changes anything. – Can Poyrazoğlu Feb 18 '15 at 17:53
  • work for me ```automaticallyAdjustsScrollViewInsets = false``` – ober Jul 13 '16 at 07:38

3 Answers3

6

If you're using Safe Area Layout Guides you can do this completely in Interface Builder.

enter image description here

Pin the view you want under the status bar to the main view using the Top Space to Container Margin constraint instead of Top Space to Safe Area constraint.

Then on the Size Inspector for the main view, uncheck Safe Area Relative Margins.

enter image description here

NSExceptional
  • 1,368
  • 15
  • 12
3

After investigating tens of pages for hours, I've found an answer:

for (NSLayoutConstraint *constraint in self.view.constraints) {
    if((constraint.firstItem == self.topLayoutGuide && constraint.secondItem == self.view) ||
       (constraint.secondItem == self.topLayoutGuide && constraint.firstItem == self.view))         {
        constraint.constant = -20;
    }
}

For anyone wondering, I did not use a specific one answer, but a derived solution from this question: iOS7 - View under status bar - edgesForExtendedLayout not working.

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • When you adding a view with storyboard or code, why not setting the offset from topLayoutGuide to -20 ? – hsafarya Feb 21 '15 at 17:27
  • @hsafarya I don't know. I think my answer and what you are saying do effectively the same thing. Instead of moving the object -20pt from the guide, I am sticking the object to the guide and moving the guide itself -20pt. They would both work. – Can Poyrazoğlu Feb 21 '15 at 17:59
  • And what if status bar is 40pt, not 20? This answer will not work for X devices or any extended status bar case – iOS Unit May 02 '19 at 15:11
2

On the view controller or parent view controller you must set automaticallyAdjustsScrollViewInsets to NO. The previous answer is a bit of a hack since the framework provides a property that controls this behavior.

jfeldman
  • 414
  • 3
  • 6
  • 1
    Deprecated in iOS 13, use now the property "contentInsetAdjustmentBehavior" on your scrollable view (eg. UIScrollView, UITableView, UICollectionView...) – Macistador Feb 18 '20 at 17:18