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

发布于 2024-11-08 04:14:30 字数 235 浏览 3 评论 0原文

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

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

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

I'm using two UIButtons in a footer of a UITableView (both subviews of a single UIView). I am not using a UITableViewCell because I wanted to skin the button so it looks like the red Delete button at the bottom of some iPhone screens such as when editing a contact.

It is sized and works correctly. However, the table will resize itself on device orientation changes (landscape, portrait and so on) and the button stays its original width. I tried using autoresizing masks but nothing worked.

Is there a trick to it, or a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在深巷 2024-11-15 04:14:30

它应该与自动调整大小蒙版一起使用,我以前做过,但正确设置视图的宽度并添加正确的大小调整蒙版很重要。

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

- (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;
}

It should work with autoresizingmasks, I've done it before but it's important to set the width of your view correctly and add the correct sizingmasks.

Some sample code to show how it works. This creates two buttons resizing whem you rotate.

- (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;
}
花伊自在美 2024-11-15 04:14:30

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

Please use 2 frames for button one for landscape mode and another for portrait mode..this will work out..so when orientatin changes check whetehr its landscape or portrait and assign frame for it...check mode here
willrotatetointerfaceorientation

坏尐絯 2024-11-15 04:14:30

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

Best i've come up with is, use one button and one cell in its own section, then each will resize appropriately.

香橙ぽ 2024-11-15 04:14:30

我做的事情与你的非常相似,只是我只有一个按钮。您返回的 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;

I do something very similar to yours except I only have one button. the uiview that you are returning should be the same width as the tableView itself.

CGFloat footerWidth = mainTableView.frame.size.width;
CGRect frameTableFooter = CGRectMake(0.0, 0.0, footerWidth, 60.0);  
viewForTableFooter.frame = frameTableFooter;
// buttons addition
mainTableView.tableFooterView = viewForTableFooter;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文