我需要在 UITableView 的第一节标题中添加一个 UIButton。
这是我的代码:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIButton *createGroupBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, tableView.frame.size.width - 100, 50)];
// other code...
return createGroupBtn;
}
return nil;
}
你看,我已经为页眉的框架添加了约束。 (例如,宽度小于 tableview 的框架的 100。)
以下是我 iPhone 中的结果:
显然,红色按钮的宽度仍然等于 tableview 的宽度。
我是否遗漏了其他一些使框架约束起作用的重要代码?
最佳答案
首先,您没有添加任何约束,您只是设置了框架。节标题始终与表格宽度相同。如果您希望按钮更窄,那么您应该创建一个 UIView,将按钮添加到其中(任意大小),然后将 View 作为节标题返回。
关于ios - UITableView : set the header frame of view in UITableView doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038818/