33

I see the following selections in Storyboard for extending the edges of a UIViewController's view under navBars/tabBars:

enter image description here

But how do I set these properties globally for all of my ViewControllers in code? As opposed to manually checking/unchecking on every ViewController in Storyboard.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60

3 Answers3

80

There is a couple of new properties in iOS7 to control those settings.

edgesForExtendedLayout tells what edges should be extended (left, right, top, bottom, all, none or any combination of those). Extending bottom edge equals "Under Bottom Bars" tick, extending top edge equals "Under Top Bars" tick.

extendedLayoutIncludesOpaqueBars tells if edges should be automatically extended under the opaque bars. So if you combine those two settings you can mimic any combination of interface builder ticks in your code.

dieworld
  • 1,090
  • 9
  • 9
  • Sorry but I can't find the two, where do I need to look for them? Many thanks in advance. – Septronic Nov 03 '15 at 07:56
  • 2
    Those two are properties of each UIViewController subclass. So, if you have an instance of MainViewController called mainViewController, you just write mainViewController.extendedLayoutIncludesOpaqueBars = YES; and so on. – dieworld Nov 11 '15 at 16:20
  • This worked for me, but I had to put it in viewDidLoad. It did not work in viewWillAppear. Thanks! Swift code... self.edgesForExtendedLayout = [ UIRectEdge.top, UIRectEdge.left, UIRectEdge.right ] – Justin Domnitz Mar 24 '17 at 14:06
  • Very old trick but working in 2022. – Mostafa Al Belliehy Mar 19 '22 at 19:39
17

If you don't want to extend to any edges, just add:

let viewController = UIViewController()
viewController.edgesForExtendedLayout = []
Shaked Sayag
  • 5,712
  • 2
  • 31
  • 38
3

In Objective-C:

- (void) viewDidLoad {
   [super viewDidLoad];
   [self initVars];
}

- (void) initVars {
   self.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeRight | UIRectEdgeBottom;
   self.extendedLayoutIncludesOpaqueBars = YES;
}

The properties that you want is:

self.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom;
jordiz
  • 279
  • 3
  • 8