2013-03-04 34 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