2010-07-19 73 views
5

我想在UITableView的頁腳中顯示UIButton(應該與頁眉完全相同)。UITableView tableFooterView奇怪的調整大小

此代碼是我TableViewController的viewDidLoad中:

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); 

但是,此代碼不能正常工作。 Button的寬度太大了,它是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]; 

這個作品中,按鈕有正確的寬度。

但是,我不明白爲什麼第一個代碼示例不起作用。誰能解釋一下?

回答

19

無論您將視圖添加爲UITableView頁腳/頁眉的寬度設置爲多少 - 其寬度總是與表格寬度相同。

因此,在您的情況下,您添加的視圖被調整爲CGRectMake(0, 0, 320, 50),但該按鈕保持正確的框架。按鈕的框架。根據視圖...

編輯
在頁腳/頭的幀仍然是你設置的是高度的唯一參數。

注意如果您將在添加到表格後更改頁眉/頁腳視圖的高度,則不會進行任何更改。在將其添加到表格之前,您應該設置高度...

+2

沒錯,就是這樣,它調整我創建和看法,我只是沒有注意到。謝謝。我認爲應該在文檔中提到設置tableFooterView屬性調整視圖的大小,這會節省一些時間。 – avf 2010-07-19 14:26:49

3

實際上我遇到了一種嘗試克服此問題的方法;

創建一個空的UIView 320x(你的按鈕的高度),然後將按鈕置於此包含視圖中。這樣視圖內的按鈕不會被調整大小。

希望這有助於

喬納森