使导航栏在 xCode 5 / iOS7 中的状态栏后面伸展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19061069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make Navigation Bar stretch behind status bar in xCode 5 / iOS7
提问by Omar
I have followed the following tutorial to move my navigation bar down so it is not covered by the status bar in xcode 5/ios7:
我已经按照以下教程将我的导航栏向下移动,这样它就不会被 xcode 5/ios7 中的状态栏覆盖:
Status bar and navigation bar issue in IOS7
But now in iOS7 there is a blank space at the top where the status bar would be and I would like the navigation bar to fill this area too
但是现在在 iOS7 中,状态栏所在的顶部有一个空白区域,我也希望导航栏也填充该区域
For instance, Facebook/twittter/Instagram iOS7 apps have the navigation bar background behind the status bar too. How do I achieve this?
例如,Facebook/twittter/Instagram iOS7 应用程序在状态栏后面也有导航栏背景。我如何实现这一目标?
Sorry if I'm not being clear but really eager to get this sorted
对不起,如果我不清楚但真的很想得到这个排序
Thank you!
谢谢!
回答by Carien van Zyl
You do want to set the barPosition
of the UINavigationBar
.
你想设置barPosition
的UINavigationBar
。
You can do this in code:
您可以在代码中执行此操作:
Let your ViewController conform to protocol UINavigationBarDelegate
and implement positionBar: metod. (The protocol that you really need is UIBarPositioningDelegate
but UINavigationBarDelegate
does extend it.)
让你的 ViewController 符合协议UINavigationBarDelegate
并实现 positionBar: 方法。(您真正需要的协议是UIBarPositioningDelegate
但UINavigationBarDelegate
确实扩展了它。)
@interface SampleViewController () <UINavigationBarDelegate>
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
@end
@implementation SampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
_navigationBar.delegate = self;
}
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
@end
OR in Storyboard:
或在故事板中:
In the Identity Inspector of UINavigationBar
, add a User Defined runtime Attribute with KeyPath = barPosition, Type = Number, and Value = 3:
在 Identity Inspector 中UINavigationBar
,添加一个用户定义的运行时属性,其 KeyPath = barPosition、Type = Number 和 Value = 3:
回答by Jarig
If you want to stretch a UINavigationBar with a custom background-image behind the UIStatusBar in iOS 7 consider the following:
如果你想在 iOS 7 的 UIStatusBar 后面拉伸带有自定义背景图像的 UINavigationBar,请考虑以下事项:
- The UIStatusBar is transparent as it is.
- Set the barPositionproperty of the UINavigationBar to UIBarPositionTopAttached
- The UINavigationBar background-images in iOS 7 (if UIBarPositionTopAttached) have different dimensions than previous to iOS 7 and you have to use them: now the height is 64 points
- UIStatusBar 是透明的。
- 将UINavigationBar的barPosition属性设置为UIBarPositionTopAttached
- iOS 7 中的 UINavigationBar 背景图像(如果 UIBarPositionTopAttached)与 iOS 7 之前的尺寸不同,您必须使用它们:现在高度为 64 点
In code (iPhone ONLY):
在代码中(仅限 iPhone):
// Image needs 64 points height
NSString* navBarPortraitBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarPortraitBackground" ofType:@"png"];
NSString* navBarLandscapeBackgroundPath;
if(UIScreen.mainScreen.bounds.size.height == 568){
// Image needs 64 points height
navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarWideLandscapeBackground" ofType:@"png"];
} else {
// Image needs 64 points height
navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarLandscapeBackground" ofType:@"png"];
}
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarPortraitBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarLandscapeBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsLandscapePhone];
If you just want to change to background colour of the UINavigationBar it will automatically extend behind the UIStatusBar.
如果您只想更改为 UINavigationBar 的背景颜色,它将自动扩展到 UIStatusBar 后面。
In Code:
在代码中:
[UINavigationBar appearance].barTintColor = [UIColor redColor];