【问题标题】:UITableView tableFooterView weird resizingUITableView tableFooterView 奇怪的调整大小
【发布时间】:2011-03-17 21:18:17
【问题描述】:

我想在 UITableView 的页脚中显示一个 UIButton(应该与页眉完全相同)。

这段代码在我的 TableViewController 的 viewDidLoad 中:

UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];
NSLog(@"%f", footerShareButton.frame.size.width);

[self.tableView setTableFooterView:footerShareButton];

NSLog(@"%f", footerShareButton.frame.size.width);
NSLog(@"%f", self.tableView.tableFooterView.frame.size.width);

但是,此代码不起作用。 Button 的宽度太大了,是 320。(虽然高度是正确的。)第一个 NSLog 输出 70,第二个和第三个输出 320。所以看起来 setTableFooterView 方法奇怪地调整了按钮的大小。

我已经解决了这个问题,方法是将 UIButton 放在另一个 UIView 中,然后再将其设置为表格页脚:

 UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];

 UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 70, 50)];
 [aView addSubview:footerShareButton];

 [self.tableView setTableFooterView:aView];
 [aView release];

这行得通,按钮的宽度正确。

但是,我不明白为什么第一个代码示例不起作用。谁能解释一下?

【问题讨论】:

    标签: iphone header uitableview footer


    【解决方案1】:

    无论您为要添加为UITableView 页脚/页眉的视图设置什么宽度 - 它的宽度始终与表格的宽度相同。

    因此,在您的情况下,您添加的视图的大小已调整为 CGRectMake(0, 0, 320, 50),但按钮仍保留在正确的框架中。按钮的框架是根据视图...

    编辑
    页脚/页眉框架中唯一保留的参数是高度。

    注意,如果您在将页眉/页脚视图添加到表格后更改其高度,则不会进行任何更改。您应该在将其添加到表格之前设置高度...

    【讨论】:

    • 是的,就是这样,它也调整了我创建的视图的大小,我只是没有注意到。谢谢你。我认为应该在文档中提到设置 tableFooterView 属性会调整视图的大小,这可以节省我一些时间。
    【解决方案2】:

    实际上我遇到了一种方法来解决这个问题;

    创建一个空的 UIView 320x(按钮的高度),然后将按钮置于此包含视图的中心。这样视图内的按钮就不会被调整大小。

    希望这会有所帮助,

    乔纳森

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2015-11-07
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多