0

For some reason, my UIWindow's rootViewController is having its margins set to 16 on the left and right, and 0 on the top and bottom. Unfortunately I'm working with a legacy (read: non-storyboard) application that initializes everything manually. This affects view controllers many layers deep, which insist on having a bottom margin of 0.

I have this code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Code

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[UIViewController alloc] init];
[self.window makeKeyAndVisible];

UIEdgeInsets insets = self.window.layoutMargins;
NSLog(@"Left %f, Right %f, Bottom %f, Top %f", insets.left, insets.right, insets.bottom, insets.top);
insets = self.window.rootViewController.view.layoutMargins;
NSLog(@"Left %f, Right %f, Bottom %f, Top %f", insets.left, insets.right, insets.bottom, insets.top);

Producing the following output

2016-08-03 18:10:24.280 app[426:182429] Left 8.000000, Right 8.000000, Bottom 8.000000, Top 8.000000
2016-08-03 18:10:24.280 app[426:182429] Left 16.000000, Right 16.000000, Bottom 0.000000, Top 0.000000

Edit

I have finally found that this is the expected behavior. When I push a new UIViewController, though, I am surprised that no bottom margin is added. Is this expected behavior? Should I never expect to have a bottom margin in iOS?

JustinHK
  • 758
  • 1
  • 6
  • 19

1 Answers1

0

The margins you are describing are nothing more than "guides". They do not affect the layout of your views unless you use them as part of your layout constraints. They do not affect the layout at all if you decide to use frames/bounds.

bsarrazin
  • 3,990
  • 2
  • 18
  • 31
  • I was relying on the layoutMargins in laying out my view, and I found it odd that buttons were being placed at the bottom of the view, but set in from the outside. I think this is expected, but if you're building with a storyboard vs. with a XIB, it will offer to use the bottom & top layout guides, which are inset as well. Seems like something legacy going on. – JustinHK Aug 04 '16 at 18:22
  • Make sure you understand clearly the difference between using AutoLayout vs using Frames/Bounds. Your expectations of margin guidelines will differ from one system to the other. – bsarrazin Aug 04 '16 at 19:20
  • I have only ever used autolayout, I was just surprised that if I create a single-page application in iOS and lock the only subview to the top, bottom, and side margins, that it doesn't touch the edge, but does touch the top and bottom of the screen. – JustinHK Aug 05 '16 at 15:57