【问题标题】:how to change height between UITableView and Navigation Bar如何改变 UITableView 和导航栏之间的高度
【发布时间】:2013-09-24 20:17:03
【问题描述】:

通过故事板的更改获得UITableViewStyleGroupedUITableViewStylePlain,发现普通表格视图的顶部边缘粘在导航栏上,而分组样式的顶部间隙不知何故由于标题视图。

但是,如图所示,间隙“a”大于“b”,为什么呢? “a”周围是否有任何隐藏元素?如何管理这个差距,以便它也可以被 bar 卡住?

间隙“a”和“b”的默认大小是多少?如何让“a”等于“b”,比如“设置”

下面是我的试用


尝试设置heightForHeaderInSection:viewForHeaderInSection:,如下所示

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * header = [[UIView alloc] init];
header.backgroundColor = [UIColor redColor];
[self.view addSubview:header];

return header;
}


试过heightForFooterInSection:viewForFooterInSection:,如下所示

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView * footer = [[UIView alloc] init];
footer.backgroundColor = [UIColor yellowColor];
[self.view addSubview:footer];
return footer;
}


看起来它们都没有按预期工作,间隙“a”始终存在并且没有改变。

奇怪的是,页眉和页脚的高度仍然存在,看起来是最小高度,

即使将它们的高度设置为零。

【问题讨论】:

  • 我遇到了同样的问题。我的结论是,如果您返回 0,则默认显示具有默认高度的标准空部分标题。它甚至不会调用标题视图和标题文本的方法。如果您返回不同的值,例如0.5,然后它调用标题视图和文本方法并显示具有指定高度的正确节标题。

标签: ios objective-c uitableview uinavigationbar ios7


【解决方案1】:

不确定为什么会出现您的问题,但有一种解决方法可以将分组的表格视图“卡”到导航栏的底部:

CGFloat myInset = *HEIGHT OF GAP A*
self.tableView.contentInset = UIEdgeInsetsMake(-myInset, 0, 0, 0);

【讨论】:

  • 试试你的代码,它看起来有效!谢谢!但是为什么会有差距“a”?默认分组表视图的正确外观是什么?你知道页脚/页眉的默认高度是多少
  • 啊...我在情节提要中找到了它们,页眉/页脚是10。但是,仍然不知道间隙“a”的含义。你能给我更多的信息吗?谢谢!
  • 对于页眉/页脚的默认值,我在情节提要中找到了它们,但仍然找不到间隙“a”的含义。无论如何,解决方法是一种权衡选择。谢谢你,@AndrewRobinson!
【解决方案2】:

只需在ViewDidLoad方法中添加这段代码:

self.automaticallyAdjustsScrollViewInsets = NO;

【讨论】:

    【解决方案3】:

    希望对你有帮助。我认为您需要更改页脚高度。

    实现那些 UITableView-delegate 方法:

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
    
        return 50.0f;
    }
    

    或者这个委托方法

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
     - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    
     - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    

    【讨论】:

      【解决方案4】:

      这是我在第一个测试版中打开的错误。同时它正在使用这个选项:

      if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
              if([UIScreen mainScreen].bounds.size.height == 568.0) {
                  //iphone5
                  sectionsTableView.frame = CGRectMake(0, 0, 320, 568);
              }
              else {
                  //iphone4s
                  sectionsTableView.frame = CGRectMake(0, 0, 320, 480);
              }
          }
      

      错误报告问题 #14310190

      【讨论】:

      • @iOS.dev 在情节提要中全屏调整您的 tableview 大小。
      • 是的。表格视图是全屏的。实际上,除了单元格行高、页眉/页脚之外,我没有找到改变 tableview 大小的地方
      • @iOS.dev 在界面构建器中。照常。您的问题是您的表格视图似乎没有全屏显示。它将与我的代码一起使用。我在第一个 beta Xcode 5 中遇到了同样的问题并打开了一张票。它仍然对 Apple 支持开放。
      • 所以你的意思是,即使在官方 Xcode 5 中,这个问题仍然存在?
      • @iOS.dev 是的。我的票还开着
      【解决方案5】:

      在每个部分之前,您都有一个标题。在每个部分之后,您都有一个页脚。您标记为“a”的是第 0 节的页眉。您标记为“b”的是第 0 节的页脚和第 1 节的页眉的组合。

      所以要拥有与设置应用相同的外观,您需要这样的代码:

      - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          if (section == 0)
              return 20.0f;
          return 10.0f;
      }
      
      - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          return 10.0f;
      }
      

      请注意,返回 0 将不起作用。它似乎具有表格视图恢复为某些默认空标题或部分的效果。

      【讨论】:

        【解决方案6】:

        对于那些仍然感兴趣的人,表格视图顶部有空间的原因是因为这是 TableView 的默认标题。要获得它,只需将其添加到您的 tableViewController

        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.0f)];
        

        这将把它画在视图控制器的顶部。您可以将 0.0 的值更改为您想要的任何值。

        【讨论】:

          【解决方案7】:

          返回表格视图的 heightForFooterInSection API(比如 0.1)中的最小值(不是 0)。这将从部分底部删除默认空间(大约 10 像素),如黄色和红色所示。

          【讨论】:

            猜你喜欢
            • 2011-09-29
            • 2011-04-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多