删除更多并使用滚动选项显示所有7个选项卡

时间:2012-12-06 08:45:24

标签: iphone ios ios6 uitabbarcontroller

我是iOS的新手。我正在使用UITabBarController创建iPhone应用程序。由于有5个标签项的限制,我在屏幕底部看到More...选项。

我想要的是显示底部的所有7个标签栏项目。主屏幕上的前5个并且有滚动选项,这样如果我滚动标签栏,我也可以看到第6和第7个标签。

有任何想法/建议如何完成这项工作?


编辑1

-(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 setTag:1]; // 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

    // Now we repeat the process for the other buttons
    btnImage = [UIImage imageNamed:@"NavBar_02.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"];
    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(80, 430, 80, 50);
    [btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn2 setTag:2];

    btnImage = [UIImage imageNamed:@"NavBar_03.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"];
    self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(160, 430, 80, 50);
    [btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn3 setTag:3];

    btnImage = [UIImage imageNamed:@"NavBar_04.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"];
    self.btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame = CGRectMake(240, 430, 80, 50);
    [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn4 setTag:4];

    btnImage = [UIImage imageNamed:@"NavBar_05.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_05_s.png"];
    self.btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn5.frame = CGRectMake(340, 430, 80, 50);
    [btn5 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn5 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn5 setTag:5];

    scrlview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 430, 400, 180)];
    // set frame accordingly its random value
    scrlview.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    scrlview.contentSize = CGSizeMake(0, 430);

    // Add my new buttons to the view
    [scrlview addSubview:btn1];
    [scrlview addSubview:btn2];
    [scrlview addSubview:btn3];
    [scrlview addSubview:btn4];
    [scrlview addSubview:btn5];
    [self.view addSubview:scrlview];
    [scrlview release];

    // Add my new buttons to the view
//  [self.view addSubview:btn1];
//  [self.view addSubview:btn2];
//  [self.view addSubview:btn3];
//  [self.view addSubview:btn4];
//  [self.view addSubview:btn5];
//    [self.view addSubview:]
//    [scrlview addSubview:];


    // Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
    [btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn5 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

2 个答案:

答案 0 :(得分:1)

创建自定义标签栏 的·H

@interface CustomTabBar : UITabBarController {
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;
    UIButton *btn4;
   UIScrollView * scrlview;
}

-(void) hideTabBar;
-(void) addCustomElements;
@end

<强>的.m

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

    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 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

        // Now we repeat the process for the other buttons
    btnImage = [UIImage imageNamed:@"NavBar_02.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"];
    btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(80, 430, 160, 50);
    [btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn2 setTag:1];

    btnImage = [UIImage imageNamed:@"NavBar_03.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"];
    btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn3.frame = CGRectMake(160, 430, 80, 50);
    [btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn3 setTag:2];

    btnImage = [UIImage imageNamed:@"NavBar_04.png"];
    btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"];
    btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn4.frame = CGRectMake(240, 430, 80, 50);
    [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
    [btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [btn4 setTag:3];

        // Add my new buttons to the view
    [scrlview addSubview:btn1];
    [scrlview addSubview:btn2];
    [scrlview addSubview:btn3];
    [scrlview addSubview:btn4];
    [self.view addSubView: scrlview];

        // Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
    [btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)selectTab:(int)tabID
{
    switch(tabID)
    {
        case 0:
            [btn1 setSelected:true];
            [btn2 setSelected:false];
            [btn3 setSelected:false];
            [btn4 setSelected:false];
            break;
        case 1:
            [btn1 setSelected:false];
            [btn2 setSelected:true];
            [btn3 setSelected:false];
            [btn4 setSelected:false];
            break;
        case 2:
            [btn1 setSelected:false];
            [btn2 setSelected:false];
            [btn3 setSelected:true];
            [btn4 setSelected:false];
            break;
        case 3:
            [btn1 setSelected:false];
            [btn2 setSelected:false];
            [btn3 setSelected:false];
            [btn4 setSelected:true];
            break;
    }   

    self.selectedIndex = tabID;

}
- (void)buttonClicked:(id)sender
{
    int tagNum = [sender tag];
    [self selectTab:tagNum];
}

请参阅Link

答案 1 :(得分:0)

您可以通过继承UITabBar并实现自己的自定义视图来实现此目的。应该相对简单。你可能会发现在UITabbar的子类化中有一些很好的信息...... How can I create my own UITabBar?