我将一个圆角矩形按钮放入表格视图页脚。
但无论我如何更改BUTTON_WIDTH,按钮都会向左和向右拖动。但它的高度是可调的。
有什么问题?以下是我使用的代码。
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 45
- (void)viewDidLoad {
CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
// btnSeeResult is decleared in header file
btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
[btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
btnSeeResult.frame = frame;
self.tableView.tableFooterView = btnSeeResult;
}
答案 0 :(得分:3)
这是因为您将按钮设置为tableView.tableFooterView
,因此视图的宽度始终设置为tableView
的宽度。
尝试使用黑色UIView
作为页脚视图,然后添加按钮,如下所示:
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 45
- (void)viewDidLoad {
CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
// btnSeeResult is decleared in header file
UIButton *btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
[btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
btnSeeResult.frame = frame;
// the width (320.0) actually doesn't matter here
UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 320.0)] autorelease];
footerView.backgroundColor = [UIColor clearColor];
[footerView addSubview:btnSeeResult];
self.tableView.tableFooterView = footerView;
}
答案 1 :(得分:2)
@SeniorLee根据您在评论中留下的问题 -
当您使用按钮作为页脚视图时,它无法工作的原因是因为页脚视图必须是表视图的宽度。这就是为什么当您的按钮是您的页脚视图时,它遵循规则,它必须是表格的宽度。
现在,当您使用常规的旧UIView时,它取代了footerview,它的宽度与表格的宽度相同。那么,您添加到该视图的任何内容都可以是您想要的。这有意义吗?
高度可以更改的原因是因为footerview的高度与宽度没有相同的强制性规定,因此您可以根据需要更改页脚的高度,而不是宽度。