2013-11-03 16 views
-1

我创建了我的第一个iOS7 SDK的应用程序,一个没有Storyboard的“空应用程序”。 状态栏总是覆盖所有其他视图。 因此,我添加了此代码:状态栏在iOS7中没有Storyboard的视图

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

但它没有改变。我的完整代码:

- (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]; 
} 
+0

你想隐藏(或摆脱)状态栏,或者只是让你的视图正确地位于状态栏的下方? –

+0

状态栏下方的视图,如iOS6上。 – Sony

+0

是您的问题通过回答[此潜在的重复问题](http://stackoverflow.com/questions/18912290/how-to-present-a-view-controller-on-ios7-without-the-status-bar-overlapping )??? –

回答

1

edgesForExtendedLayout仅适用于存在UI容器视图控制器(如UINavigationController)的情况。为了避免重叠,你应该使用-topLayoutGuide(它也存在一个底部布局指南)。我制作了一个gist on github,它使用容器视图作为此布局集的vc主视图的子视图。

//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]; 

} 

这个片段进行控制,看到的是我们有一个topLayoutGuide,以后它会删除相关的上海华上tankView的约束(即增加并连接在厦门国际银行的容器视图),并增加了新的限制基于topLayoutGuide。