gpt4 book ai didi

iphone - 当方向改变时如何调整 UITableView 页脚中 UIButton 的大小

转载 作者:行者123 更新时间:2023-12-03 18:32:32 25 4
gpt4 key购买 nike

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

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

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

最佳答案

它应该与 autoresizingmasks 一起使用,我之前已经这样做过,但正确设置 View 的宽度并添加正确的 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;
}

关于iphone - 当方向改变时如何调整 UITableView 页脚中 UIButton 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6042949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com