9

我在 UITableView 的页脚中使用了两个 UIButton(单个 UIView 的两个子视图)。我没有使用 UITableViewCell,因为我想为按钮设置皮肤,使其看起来像某些 iPhone 屏幕底部的红色删除按钮,例如在编辑联系人时。

它的大小和工作正常。但是,表格将在设备方向更改(横向、纵向等)时自行调整大小,并且按钮保持其原始宽度。我尝试使用自动调整大小的蒙版,但没有任何效果。

有没有什么技巧,或者更好的方法?

4

4 回答 4

14

它应该与 autoresizingmasks 一起使用,我以前做过,但是正确设置视图的宽度并添加正确的 sizingmasks 很重要。

一些示例代码来展示它是如何工作的。这会创建两个按钮来调整您旋转的大小。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];

    view.backgroundColor = [UIColor redColor];

    UIButton *buttonA = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA.frame = CGRectMake(20, 5, 125, 40);
    [buttonA setTitle:@"ButtonA" forState:UIControlStateNormal];
    buttonA.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [view addSubview:buttonA];

    UIButton *buttonB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB.frame = CGRectMake(175, 5, 125, 40);
    [buttonB setTitle:@"ButtonB" forState:UIControlStateNormal];
    buttonB.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [view addSubview:buttonB];

    return [view autorelease];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
于 2011-05-24T20:06:16.320 回答
0

我做的事情和你的很相似,只是我只有一个按钮。您返回的 uiview 应该与 tableView 本身的宽度相同。

CGFloat footerWidth = mainTableView.frame.size.width;
CGRect frameTableFooter = CGRectMake(0.0, 0.0, footerWidth, 60.0);  
viewForTableFooter.frame = frameTableFooter;
// buttons addition
mainTableView.tableFooterView = viewForTableFooter;
于 2011-05-24T19:51:30.183 回答
0

请为按钮使用 2 个框架,一个用于横向模式,另一个用于纵向模式..这将解决..所以当方向更改时,请检查其横向或纵向并为其分配框架...此处检查模式将旋转到界面方向

于 2011-05-18T10:14:15.177 回答
0

我想出的最好的方法是,在自己的部分中使用一个按钮和一个单元格,然后每个单元格都会适当地调整大小。

于 2011-05-24T15:34:56.437 回答