【问题标题】:Custom NavigationBar in UINavigationcontrollerUINavigationcontroller 中的自定义 NavigationBar
【发布时间】:2013-04-02 18:13:01
【问题描述】:

我在 UINavigationcontroller 中有一个 UITableViewController,我想在其中添加一个自定义导航栏。如果我在 tableView 类中执行此操作,它会与表格一起滚动,这不是我想要的。如果我在 NavigationController 类中执行此操作,它不会出现。

我在 NavigationController NIB 文件中创建了一个 UINavigatioBar,并将其作为 IBOutlet 连接到我的 Navigationcontroller.h。在我的 NavigationController.m 我有:

productsNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1048, 88)]; [productsNavigationBar setBarStyle:UIBarStyleBlackTranslucent]; [self.view addSubview:productsNavigationBar];

谁知道怎么做才能让我的酒吧出现?

【问题讨论】:

  • 如果你只在 UITableViewController 中使用它,那么将你的内容添加到 UITableView 的 Header 中它会解决你的问题
  • 嗨@Joris Dijkstra,还记得我吗?
  • 我的 tableView 单元格不仅仅是具有静态内容的单元格,它们还包含许多文本字段。我想为用户提供一些工具,如添加/删除行、更改字体等,所以我认为只有 2 个 leftBarButton 和 rightBarButton 就足够了。
  • 是的@DipenPanchasara,我记得你。你对表格/导航问题帮助很大
  • 我在下面发布了一些代码,试试这个并根据您的要求在该标题视图中添加按钮,它可能会解决您的问题..!

标签: ios objective-c xcode ipad xcode4.5


【解决方案1】:

在你的 tableViewController 中实现这个 DataSource 方法,并在 UITableView 的 Header 中添加你想要的导航栏

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    [headerView setBackgroundColor:[UIColor redColor]];

    return headerView;
}

改变Header的高度实现这个方法

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100.0f;
}

【讨论】:

  • 您的代码确实添加了节标题,但高度似乎无法自定义。当我将“30”更改为“100”时,高度不会改变。我需要自定义 NavigationBar。
  • 我实际上可以添加自定义 UINavigationBar,但只能在我的 tableview 中。当我这样做时,它会随着 tableView 向上滚动(我在我的问题中使用了代码)。你知道我可以在 Navigationcontroller 中添加这样一个自定义栏的方法,以防止它与 tableView 一起滚动吗?
  • 谢谢!我已经在寻找这样的方法了。让我尝试添加自定义背景图片和一些按钮。
  • 所以无法在 NavigationController 中添加自定义导航栏?
  • 它不是自定义导航栏,它只是 UITableView 中的一个标题视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多