如何在方向更改时调整UITableView页脚中的UIButton大小

aka*_*aru 9 iphone footer uitableview uiinterfaceorientation

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

它的大小和工作正常.但是,该表将根据设备方向更改(横向,纵向等)调整其大小,并且按钮保持其原始宽度.我尝试过使用autoresizing mask但没有任何效果.

它有诀窍,还是更好的方法?

Mac*_*n13 14

它应该与autoresizingmasks一起使用,我之前已经完成了,但是正确设置视图的宽度并添加正确的大小调整是很重要的.

一些示例代码,以显示它是如何工作的.这会创建两个按钮,可以在旋转时调整大小.

- (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;
}
Run Code Online (Sandbox Code Playgroud)