UITableView tableFooterView奇怪的调整大小 [英] UITableView tableFooterView weird resizing

查看:261
本文介绍了UITableView tableFooterView奇怪的调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码位于我的TableViewController的viewDidLoad中:

我想在UITableView的页脚中显示一个UIButton(应该和页眉完全一样) / p>

  UIButton * footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[footerShareButton setFrame:CGRectMake(10,0,70,50)];
NSLog(@%f,footerShareButton.frame.size.width);

[self.tableView setTableFooterView:footerShareButton];

NSLog(@%f,footerShareButton.frame.size.width);
NSLog(@%f,self.tableView.tableFooterView.frame.size.width);

但是,这段代码不起作用。该按钮的宽度太大,它是320.(虽然高度是正确的)。第一个NSLog输出70,第二个和第三个输出320.所以看起来像setTableFooterView方法奇怪地调整按钮大小。



我已经通过将UIButton放置在另一个UIView中,然后将其设置为表格页脚来解决此问题:

  UIButton * footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[footerShareButton setFrame:CGRectMake(10,0,70,50)];

UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(10,0,70,50)];
[aView addSubview:footerShareButton];

[self.tableView setTableFooterView:aView];
[aView release];

这可以起作用,按钮的宽度是正确的。



然而,我不明白为什么第一个代码示例不起作用。任何人都可以解释吗?

解决方案

无论您将视图添加为 UITableView footer / header - 它的宽度总是与表格宽度相同。

因此,在你的情况下,添加被调整大小为 CGRectMake(0,0,320,50),但该按钮保持正确的框架。按钮的框架根据视图...



编辑
页脚/页眉框架中的唯一参数


请注意,如果您在添加页眉/页脚视图后将更改其高度到桌上它不会做任何改变。您应该在将其添加到表格之前设置高度...


I want to display a UIButton in a UITableView's Footer (should be exactly the same for the header).

This code is in the viewDidLoad of my TableViewController:

UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];
NSLog(@"%f", footerShareButton.frame.size.width);

[self.tableView setTableFooterView:footerShareButton];

NSLog(@"%f", footerShareButton.frame.size.width);
NSLog(@"%f", self.tableView.tableFooterView.frame.size.width);

However, this code doesn't work. The Button's width is much too large, it's 320. (The height is correct though.) The first NSLog outputs 70, the second and the third output 320. So it seems like the setTableFooterView method strangely resizes the button.

I have solved this problem by putting the UIButton in another UIView before setting it as the table footer:

 UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];

 UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 70, 50)];
 [aView addSubview:footerShareButton];

 [self.tableView setTableFooterView:aView];
 [aView release];

This works, the button has the correct width.

However, I don't understand why the first code sample doesn't work. Can anyone explain?

解决方案

No matter what width you set to the view that you are adding as a UITableView footer/header - its width always will be the same as the table's width.

So, in your case the view that you've added is resized to CGRectMake(0, 0, 320, 50), but the button remains with the correct frame. Button's frame is according to the view...

EDIT:
The only parameter in footer's/header's frame that remains as you set it is the height.

Notice that if you will change the height of the header/footer view after it is added to the table it won't make any change. You should set the height before adding it to the table...

这篇关于UITableView tableFooterView奇怪的调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆