【问题标题】:Make Navigation Bar stretch behind status bar in xCode 5 / iOS7使导航栏在 xCode 5 / iOS7 中的状态栏后面伸展
【发布时间】:2013-10-04 08:37:40
【问题描述】:

我已按照以下教程将导航栏向下移动,因此它不会被 xcode 5/ios7 中的状态栏覆盖:

Status bar and navigation bar issue in IOS7

但现在在 iOS7 中,状态栏所在的顶部有一个空白区域,我希望导航栏也填充该区域

例如,Facebook/twittter/Instagram iOS7 应用程序在状态栏后面也有导航栏背景。我如何做到这一点?

对不起,如果我不清楚,但真的很想解决这个问题

谢谢!

【问题讨论】:

  • 您是使用自定义导航栏还是内置导航栏?

标签: iphone objective-c xcode uiviewcontroller ios7


【解决方案1】:

您确实想设置UINavigationBarbarPosition

您可以在代码中执行此操作:

让你的 ViewController 符合协议UINavigationBarDelegate 并实现 positionBar:metod。 (您真正需要的协议是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:

UINavigationBar的Identity Inspector中,添加一个User Defined runtime Attribute,KeyPath = barPosition,Type = Number,Value = 3:

【讨论】:

  • 非常感谢您教我用户定义的运行时属性菜单的作用。这对我来说是惊天动地的。我永远不会子类化一个 ui 元素来再次更改一个属性。
  • 这里很有趣,但它对我不起作用。我总是收到以下错误:“***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在控制器管理的 UINavigationBar 上手动设置委托。'”如果我覆盖 UINavigationViewController 并将其添加到那里,我会这样做没有得到错误,但它也不起作用......
  • @lol 我只是希望你在讽刺,哦,这根本不是你应该养成的习惯。它很快就会成为调试的噩梦。
  • @TheCodingArt 是的,我在 Quora 上读到 Uber 工程师的一些东西,他们说他们从 Xcode 中删除了所有图形 UI 设计工具——正因为如此; Xcode 存储和解析文件的方式导致了版本控制的噩梦。该评论肯定有 4 票赞成,但我认为这些应该被视为“紧急情况”,例如单一作者/模型/非生产应用程序!
  • 有趣的是有多少工程师讨厌故事板并抱怨他们在测试和调试方面很糟糕......然后你打开他们的代码,他们有大量的胶水代码和样板绝对无法解析
【解决方案2】:

如果您想在 iOS 7 的 UIStatusBar 后面使用自定义背景图像拉伸 UINavigationBar,请考虑以下事项:

  1. UIStatusBar 是透明的。
  2. 将 UINavigationBar 的 barPosition 属性设置为 UIBarPositionTopAttached
  3. iOS 7 中的 UINavigationBar 背景图像(如果 UIBarPositionTopAttached)与 iOS 7 之前的尺寸不同,您必须使用它们:现在 高度为 64 磅

在代码中(仅限 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];

如果您只想更改 UINavigationBar 的背景颜色,它将自动延伸到 UIStatusBar 后面。

在代码中:

    [UINavigationBar appearance].barTintColor = [UIColor redColor];

【讨论】:

  • 您可能想解释一下如何实际设置 barPosition。
猜你喜欢
  • 2013-09-14
  • 2013-09-29
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多