2013-03-04 58 views
0

在StackOverflow上搜索幾個問題後,我發現只有1個主要項目用於創建自定義UITabBar,名爲BCTabBarController。它的描述說:實現不使用私有API的自定義UITabBarControl

有幾個問題與使用標準的UITabBarController 包括:

這是太高,尤其是在橫向模式下

高度不匹配UIToolbar

不能在不使用專用的API

定製210

儘管如此,我發現this strange project on GitHubtutorial here,使用標準UITabBarController在其實施UIButtons爲每個選項卡,它的工作(奇怪的是,但它)。

我想知道,如果這是錯誤的創建自定義UITabBarControllerUIButtons,而不是標籤和那會導致成?這樣做的實施看起來是這樣的:

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

    [self hideTabBar]; 
    [self addCustomElements]; 
} 

- (void)hideTabBar 
{ 
    for(UIView *view in self.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      view.hidden = YES; 
      break; 
     } 
    } 
} 

-(void)addCustomElements 
{ 
    // Initialise our two images 
    UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"]; 
    UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"]; 

    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button 
    btn1.frame = CGRectMake(0, 430, 80, 50); // Set the frame (size and position) of the button) 
    [btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button 
    [btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button 
    btn1.backgroundColor = [UIColor yellowColor]; 
    [btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed. 
    [btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially 

在我的項目,我將使用的是iOS 5.1及以上,沒有故事板或XIBs。謝謝!

回答

1

從iOS 5.0開始,在屏幕底部使用UIButtons行創建自己的UITabBarController已不再是個問題。

在以前的iOS SDK版本中,由於您必須自己管理viewWill/viewDid方法的轉發,所以存在一些風險。

看一看在UIViewController類參考,部分實現一個容器視圖控制器,你會發現你所需要的有:UIViewController Class Reference

還有一個有特色的文章正好說明你需要:Creating Custom Container View Controllers

希望這會有所幫助,

+0

感謝您指點我正確的方向! – 2013-03-04 14:36:04