自动布局
,那么您需要做的就是从最上面的视图添加一个垂直约束
到顶部布局指南
,如下所示,它会处理顶部间距。
更多信息请参考: https://developer.apple.com/library/ios/qa/qa1797/_index.html
以下是我为了始终将视图的顶部填充20像素(状态栏的高度)而采取的措施。
我在AppDelegate的application:didFinishLaunchingWithOptions方法中使用了以下代码:
...
// container holds my root view controller
UINavigationController *container = [UINavigationController alloc] init...];
...
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // iOS 7
// Create parent controller that will contain your existing root view controller's view shifted 20 px down to account for the status bar.
UIViewController *newRootController = [[UIViewController alloc] init];
// Add my old root view controller as a child
[newRootController addChildViewController:container];
// Add its view as a subview
[newRootController.view addSubview:container.view];
// Call this method because it does some configuration?
[container didMoveToParentViewController:newRootController];
// Now just position the view starting at point (0, 20)
UIView *aView = container.view;
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(aView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[aView]|" options:0 metrics:nil views:viewDictionary];
[newRootController.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aView]|" options:0 metrics:nil views:viewDictionary];
[newRootController.view addConstraints:constraints];
self.window.rootViewController = newRootController;
} else { // pre iOS 7
self.window.rootViewController = container;
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
//Added on 19th Sep 2013
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
return YES;
在iOS 7中,您可以通过设置以下内容来禁用视图进入顶部栏:
if([controller canPerformAction:@selector(setEdgesForExtendedLayout:) withSender:self]) {
[controller setEdgesForExtendedLayout:UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight];
}