2013-10-11 141 views
30

我有我添加到一個TabBarController與標籤欄覆蓋的TableView細胞iOS7

self.tabBarController.viewControllers = [NSArray arrayWithObjects:someOtherViewController, customTableViewController, nil]; 
self.tabBarController.selectedIndex = 1; 

我的是,最後的1.5 tableViewCells被覆蓋的標籤欄時遇到的問題定製tableViewController在運行iOS7的iPhone 4屏幕的底部。當我使用iOS模擬器 - iPhone Retina(4英寸)/ iOS 7.0時,問題仍然存在。

是什麼使的tableView線向上與在屏幕的底部的TabBar的頂部,而無需使用「幻數」的正確方法?

+0

我建議你創造的UIViewController視圖控制器子類,並添加泰伯維IBOutlate。通過xib或storyBoard,並添加該創建viewController到tabbar控制器 –

+0

這正是現在的設置 – JuJoDi

回答

77

試試這個你CustomViewController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIEdgeInsets adjustForTabbarInsets = UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.tabBarController.tabBar.frame), 0); 
    self.scrollView.contentInset = adjustForTabbarInsets; 
    self.scrollView.scrollIndicatorInsets = adjustForTabbarInsets; 
} 
+1

我已經試過了,它似乎提高了tableview綁定,但仍然約覆蓋一半的表視圖單元格。 – JuJoDi

+0

查看已更新的答案 – dariaa

+0

不,不,它是contentInset。這實際上是一個UIScrollView屬性,因爲UITableView是UIScrollView的一個子類,所以你應該始終能夠編輯它的contentInset – dariaa

6

設置你的表視圖的contentInset與49點.bottom值應該糾正。

在適當的配置,在iOS 7的新的UIViewController屬性設置YES稱爲automaticallyAdjustsScrollViewInsets應該糾正這一點,但(再次)它取決於很多其他因素(視圖層次,父視圖控制器的設置,等等)。

+0

根據[Apple的文檔](https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html),沒有像automaticallyAdjustsContentInsets屬性那樣的東西 – JuJoDi

+1

它是'自動調整ScrollViewInsets' , 我很抱歉。 – jaredsinclair

6

接受的答案不適合我 - 我的設置有點不同。我正在編程創建我的視圖控制器。我的應用程序的根是一個標籤欄控制器,一個選項卡是一個導航控制器,其根是一個UIViewController與表視圖作爲主視圖。

什麼雖然我工作原理是當我手動計算表視圖的高度,並將其設置在框架時ALLOC-INITING表視圖。一般的計算公式爲:

屏幕高度 - (狀態欄高度+導航欄高度+標籤欄高度)

+0

我與馬特有相同的設置,不知何故,這仍然適用於iOS 9+ – jaytrixz

0

我認爲這將更好地爲您:

[super viewDidLoad];

試試下面的代碼:

if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) 
    self.edgesForExtendedLayout = UIRectEdgeNone; 
1

我有類似的視圖層次到Matt Quiros:UITabBarController - > UINavigationController - > UIViewController - > UITableViewController(作爲UIViewController的子視圖嵌入)。其他答案在我的情況下不起作用,我必須在表視圖控制器的viewWillAppear:方法中手動設置表視圖的框架。

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // Adjust height of tableview (does not resize correctly in iOS 7) 
    CGRect tableViewFrame = self.tableView.frame; 
    tableViewFrame.size.height = [self heightForTableView]; 
    self.tableView.frame = tableViewFrame; 
} 

- (CGFloat)heightForTableView 
{ 
    return CGRectGetHeight([[UIScreen mainScreen] bounds]) - 
      (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + 
      CGRectGetHeight(self.navigationController.navigationBar.frame) + 
      CGRectGetHeight(self.tabBarController.tabBar.frame)); 
} 

如果有人找到更好的解決方案,請分享!

5
CGFloat bottom = self.tabBarController.tabBar.frame.size.height; 
NSLog(@"%f",bottom); 
[self.tableview setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, bottom, 0)]; 
self.tableview.contentInset = UIEdgeInsetsMake(0, 0, bottom, 0); 
2

將您的表格控制器嵌入到導航控制器中。 1.選擇故事板中的視圖。 2.在菜單欄上選擇編輯器 - >嵌入 - >導航控制器。

希望幫助

0

您還可以實現viewDidLayoutSubviews和使用bottomLayoutGuide獲得標籤欄的高度:

- (void)viewDidLayoutSubviews 
{ 
    [super viewDidLayoutSubviews]; 

    CGFloat bottomOffset = self.bottomLayoutGuide.length; 

    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, bottomOffset, 0); 
} 
0

即使改變你的表視圖的contentInset是一個可行的解決方案,我最好確保你的表格視圖在Tabbar之前停下來。

正如Paul Newman所說,使用bottomLayoutGuide是一件好事,特別是如果您使用自動佈局。 在我的情況下,添加一個約束到底部的tableview鏈接到BottomLayoutGuide的頂部是一個乾淨的解決方案,這是一個Storyboard的例子,但它也可以在代碼中完成。

enter image description here

希望它能幫助。

47

這是一個iOS 8的解決方案,但它可能在iOS 7上工作:去故事版>選擇表視圖控制器>取消選中「底部酒吧」。而已!

example

+1

在iOS 7和8對我來說,我認爲這是更優雅的答案。 – Simon

+12

對於功能而言,它是更優雅的答案......但同時它使tabBar灰色的背景......這不是我想要的。 – Simon

+3

它是灰色的,因爲你有tabBar半透明 – alessioarsuffi