2012-05-24 20 views
6

我拍了一個普通的UITabBar,並將其背景圖像更改爲自定義高度較低的圖像,所以我更改了的height。最初我得到的是標籤欄下方的空白區域。所以我也改變了frameorigin。但現在空白已經移動了標籤欄上方,這就是結果:自定義UITabBar和ViewController之間的空間

space above tab bar

這是宣佈在AppDelegate的標籤欄的代碼:

self.tabContoller = [[UITabBarController alloc] init]; 
//customizing the tabbar 
UIImage * tabBackgroundImage = [UIImage imageNamed:@"tabBarBg.png"]; 
self.tabContoller.tabBar.backgroundColor = [UIColor colorWithRed:245.f/255.f green:245.f/255.f blue:245.f/255.f alpha:255.f/255.f]; 
self.tabContoller.tabBar.backgroundImage = tabBackgroundImage; 
//setting the tabbar height to the correct height of the image 
CGRect tabR = self.tabContoller.tabBar.frame; 
CGFloat diff = tabR.size.height - tabBackgroundImage.size.height; 
tabR.size.height = tabBackgroundImage.size.height; 
tabR.origin.y += diff; 
self.tabContoller.tabBar.frame = tabR; 

我猜問題在於ViewController在常量標籤欄的高度不變的空間上繪製自己。有什麼方法可以改變它嗎?

+0

只需增加viewcontroller的view的'height'即可。如果無法形成xib,請在'viewDidLoad'中手動執行。 – Mat

+0

這不起作用,我甚至嘗試在'viewWillAppear'和'viewDidAppear'中設置它。它沒有效果。 – Nadavrbn

回答

7

更改您的UITabBarController的子視圖一個全尺寸的框架,這個工作對我來說:

[[yourTabBarController.view.subviews objectAtIndex:0] setFrame:CGRectMake(0, 0, 320, 480)]; 
+0

非常感謝您的救星! – Nadavrbn

+0

非常感謝! –

+0

這不適用於iOS7,似乎有一些額外的東西留下。黑色矩形消失,但現在有一個灰色的邊界,它曾經是... – ryan0

0

如果像上面提到的@JoaT那樣改變幀不起作用,請確保視圖控制器的視圖具有正確的自動識別掩碼集。

This SO鏈接可能會有幫助。

+0

我嘗試過'UIViewAutoresizingFlexibleHeight'和'UIViewAutoresizingFlexibleBottomMargin'並沒有得到任何結果。我還設置了這些屬性:'self.tabContoller.tabBar.autoresizesSubviews = YES;'和'self.tabContoller.view.autoresizesSubviews = YES;'並沒有幫助。我應該把它放在其他地方嗎? – Nadavrbn

+0

您是否嘗試在第一個視圖控制器出現後打印出框架並限制了選項卡欄的大小?也許這不是我們在應用程序代理中設置它之後所期望的。 – Mike

0

我試着通過改變tabbar的高度和原點,對我來說它工作正常。你可以試着增加viewcontroller的高度。

7

嘗試建立從UITabBar擴展自己的類,利用此功能:

- (CGSize)sizeThatFits:(CGSize)size { 
    CGSize auxSize = size; 
    auxSize.height = 54; // Put here the new height you want and that's it 
    return auxSize; 
} 

這將調整UITabBar的大小,你想要的。簡單而簡單。

+0

我很驚訝,我花了這麼長時間才找到這個答案。它應該更高效。這真的是對UITabBar做簡單的事情的最簡單的方法。謝謝。 –

+0

我很高興它適合你。不用謝。 – crisisGriega

+0

這實際上是我能夠在UITabBar上獲得自定義高度的唯一方法。謝謝。 –