我想在 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];
这有效,按钮具有正确的宽度。
但是,我不明白为什么第一个代码示例不起作用。谁能解释一下?