2011-05-18 28 views
9

我在UITableView的頁腳中使用了兩個UIButton(兩個UIView的子視圖)。我沒有使用UITableViewCell,因爲我想對按鈕進行蒙皮處理,使其看起來像一些iPhone屏幕底部的紅色「刪除」按鈕,例如編輯聯繫人時。如何調整方向更改時在UITableView頁腳中調整UIButton

它的大小和工作正常。但是,表格會根據設備方向更改(橫向,縱向等)調整其大小,並且按鈕保持其原始寬度。我嘗試使用自動調整掩碼,但沒有任何工作。

它有一個竅門,或更好的方法?

+0

你對'viewForFooterInSection:'返回什麼?按鈕或帶有按鈕的視圖? – 2011-05-18 10:17:42

+0

我用一個按鈕返回一個視圖(實際上是兩個按鈕)。 – akaru 2011-05-19 06:02:20

回答

13

它應與autoresizingmasks工作,我以前做過,但正確設置你的視圖的寬度,並添加正確的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; 
} 
+0

看着你的代碼,我注意到我的問題是...我沒有設置按鈕的超級視圖的框架...這就是爲什麼我的自動修復面具不工作。 – akaru 2011-05-25 04:07:19

+0

Thnx的代碼..!完美的工作...... !!! – 2013-12-27 09:09:38

0

請使用2幀的按鈕一爲橫向模式,另一種爲肖像模式..這將解決..所以當orientatin變化檢查whetehr其風景或肖像,併爲其分配框架...在這裏檢查模式 willrotatetointerfaceorientation

+0

謝謝。但是,我認爲,餐桌應該有一種方式來照顧它。 – akaru 2011-05-19 06:03:00

+0

nope..Table不會這樣做.. – Krishnabhadra 2011-05-24 05:25:36

0

我認爲最好的是,在自己的部分使用一個按鈕和一個單元格,然後每個單元格都會適當調整大小。

0

我做了一件非常類似於你的事情,除了我只有一個按鈕。你正在返回的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;