Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting "edgesForExtendedLayout = UIRectEdgeNone" does not prevent my subview from moving up in iOS 7

I have a view controller that was written back in the days of iOS 5 and I'm trying to transition it to iOS 7. After reading the iOS 7 transition guide and poking around on SO, I discovered that I need to set the new iOS 7 property edgesForExtendedLayout to UIRectEdgeNone to prevent one of my custom subviews from appearing 49 pixels higher on iOS 7 than it appears on iOS 6. However, after setting that property, my custom subview is still appearing 49 pixels higher on iOS 7 and I don't know what else I need to do. Here's my simple code that I added to my viewDidLoad method...

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

and here's the code for creating and adding the custom subview that is appearing higher on iOS 7...

CGRect customControlFrameRect = {{0.0f, 240.0f}, {100.0f, 100.0f}};
self.customControl = [[MyCustomControl alloc] initWithFrame:customControlFrameRect];

self.customControl.autoresizingMask =  UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:self.customControl];

One other important detail, if it helps, is this view is created from a nib file, but the custom subview that is appearing higher on iOS 7 than iOS 6 is the only subview that is created and added programmatically in viewDidLoad, after I set the edgesForExtendedLayout property. All the other subviews that are created from the nib aren't affected regardless of the whether or not I set the egdesForExtendedLayout property.

My two questions are...

  1. Why is my custom subview appearing higher on iOS 7 even after I set the edgesForExtendedLayout property to UIRectEdgeNone?
  2. Why aren't the other subviews (the subviews that are loaded from the nib) appearing higher in iOS 7?

Thanks in advance for your wisdom!

like image 381
BeachRunnerFred Avatar asked Dec 19 '13 19:12

BeachRunnerFred


2 Answers

1.

It is the purpose of edgesForExtendedLayout which was set to UIRectEdgeNone to not extend the view in any direction, but your view will now start in origin within the screen size (now below the statusBar!) when using a frame with position of 0, 0 (like you did with the customControl). You need to adjust the frame of your view/viewController to represent that you want to start under the statusBar/navigationBar or make the status/nagivationBar opaque; or add UIRectEdgeTop if only used for one view.

Setting the navigationController to opaque will also do so with the statusBar:

self.navigationController.navigationBar.translucent = NO;

2.

Because the interface builder sets the frame correctly under the navigationBar by default, you could change it of course. And it uses UIRectEdgeAll and autoresizing.

like image 183
Binarian Avatar answered Oct 20 '22 19:10

Binarian


I've found, in some cases, that setting the edgesForExtendedLayout in viewWillAppear: rather than viewDidLoad gives me the result I'm looking for. This may not solve your problem, but it has helped me in a few cases and hopefully it'll help others.

like image 4
Mike Avatar answered Oct 20 '22 20:10

Mike