-1

I created my first app with iOS7 SDK, a "empty application" without Storyboard. The status bar is always over all other views. So I add this code :

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;

But it changes nothing. My full code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 200, 300)];
    [v setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:v];
}
Sony
  • 1,773
  • 3
  • 23
  • 39

1 Answers1

1

edgesForExtendedLayout applies only in presence of a UI container view controller (such as UINavigationController). To avoid that overlapping, you should use -topLayoutGuide (it also exist a bottom layoutGuide). I've made a gist on github that use a container view as a subview of the vc main view with this layout set.

//This should be added before the layout of the view
- (void) adaptToTopLayoutGuide {
    //Check if we can get the top layoutguide
    if (![self respondsToSelector:@selector(topLayoutGuide)]) {
        return;
    }
    //tankView is a contaner view
    NSArray * array = [self.tankView referencingConstraintsInSuperviews]; //<--For this method get the Autolayout Demistified Book Sample made by Erica Sadun
    [self.view removeConstraints:array];
    NSArray * constraintsVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide]-0-[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView, @"topLayoutGuide":self.topLayoutGuide}];
    [self.view addConstraints:constraintsVertical];
    NSArray * constraintsHorizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView}];
    [self.view addConstraints:constraintsHorizontal];

}

This snippet make a control to see is we have a topLayoutGuide, later it removes the constraints on the tankView (that is a container view added and connected in a xib) related to the superview and it adds new constraints based on the topLayoutGuide.

Andrea
  • 26,120
  • 10
  • 85
  • 131