【问题标题】:How to replace UINavigationBar with CustomHeader in iOS7如何在 iOS7 中用 CustomHeader 替换 UINavigationBar
【发布时间】:2013-10-05 20:46:36
【问题描述】:

我想使用自定义标头而不是默认的 UINavigationBar。我能够做得很好,直到我遇到一个条件来保持与 iOS6 的向后兼容性。我有我之前的问题here未回答。

最近对于 Facebook 应用,我可以在 iOS7 中看到,蓝色标题栏对齐良好。对我来说,它看起来像是一个自定义标题,我不确定。 FaceBook-Header

现在的问题是,我无法实现自定义应用程序标头。我的自定义标题高度是 48 像素,位于顶部的“网络状态栏”后面,我只得到大约 35 像素,其余部分位于“网络状态栏”后面like this.

这是我要实现的目标。

  • 我想要一个自定义标题。 - 完成
  • 我不想使用UINavigationBar。 - 完成
  • 我希望支持 iOS 5.0 到 7.0 - 卡住了

这是我使用Custom header时的样子

这是UINavigationBar 的样子

如何使应用标题看起来像带有“自定义标题”的“FaceBook 标题”,而不会进入“顶部网络标题栏”下方。如果我增加状态栏的高度,它会在 iOS6 及更早版本中看起来很奇怪,但在 iOS7 中非常适合。我想选择正确的方法。

我们非常感谢任何专家的帮助。

【问题讨论】:

  • 你可以像这样使用 UIToolbar。
  • @karthika 你能详细说明一下吗?

标签: iphone objective-c uinavigationbar ios7


【解决方案1】:

你需要在xib中放置UIToolbar,

并在工具栏中添加背景图片,

 self.navigationController.navigationBarHidden = YES;

[self.toolBar setBackgroundImage:[UIImage imageNamed:@"background.png"] forToolbarPosition:UIToolbarPositionAny barMetrics: UIBarMetricsDefault];

添加栏按钮,

UIButton *facebookButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[facebookButton setImage:[UIImage imageNamed:@"facbookBarImage.png"] forState:UIControlStateNormal];
[facebookButton addTarget:self action:@selector(action:)forControlEvents:UIControlEventTouchUpInside];
[facebookButton setFrame:CGRectMake(0, 0, 53, 31)];
UIBarButtonItem *facebookBarButton = [[UIBarButtonItem alloc] initWithCustomView: facebookButton];

self.toolBar.items = [NSArray arrayWithObjects: facebookBarButton,nil];

【讨论】:

  • 使用UIToolBar *toolbarUIView *myCustomheader 并向myCustomheader 添加按钮有什么区别?我可以使用 UIToolBar 实现任何特殊功能吗? UIToolBar 的使用是否可以解决我的自定义标题位于“网络状态栏”下方的问题
【解决方案2】:

在对兼容性进行了一些研究后,我遇到了一个可行的解决方案,可以制作没有 UINavigationBar 的自定义标头并实现我想要的。随意使用这种方法。

这是我的结果:iOS7Older iOS

方法是找出edgesForExtendedLayout(在iOS 7 中可用)。如果我们在 iOS7 上,那么决定“Custom Header”的高度(可能比我们在旧版本中保留的多 10Pixel,以便在“Top Network status bar”下方填充额外的 10Pixel)。

  • 在这里找出edgesForExtendedLayout的iOS7兼容性

    CGFloat y;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        y= 58;
    }else {
        y = 48;
    }
    
  • 在此处制作高度为y 的自定义标题

    self.customHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appDimensions.width, y)];
    
  • customHeaderView 上的自定义按钮在这里

    self.sideMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sideMenuButton setFrame:CGRectMake(0, y-45, 45, 45)];
    

所以现在如果我在 iOS7 上运行它,我会看到我的标题有足够的 48 像素高度(比我们在触摸区域的人机界面指南 (44 x 44) 中的高度)即使是 10 像素也在“顶级网络”下方状态栏)。

如果我在 iOS

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多