我想在 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];
这有效,按钮具有正确的宽度。
但是,我不明白为什么第一个代码示例不起作用。谁能解释一下吗?
最佳答案
无论您为要添加为 UITableView
的 View 设置什么宽度,页脚/页眉 - 其宽度始终与表格的宽度相同。
因此,在您的情况下,您添加的 View 的大小调整为 CGRectMake(0, 0, 320, 50)
,但按钮仍保留在正确的框架中。按钮的框架是根据 View ...
编辑:
页脚/页眉框架中唯一保留的参数是高度。
请注意,如果您在将页眉/页脚 View 添加到表格后更改其高度,则不会进行任何更改。您应该在将其添加到表格之前设置高度...
关于iphone - UITableView tableFooterView 奇怪的调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280841/