Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UITabbarController to UIViewController in iOs

How to push Viewcontroller with TabbarController? In Viewcontroller'XIB, i created UITabbarController. Then i push this ViewController, but it not appear UITabbarController. This is my code: *Viewcontroller.h:

 @interface StatusViewController : UIViewController<UITabBarControllerDelegate>
{
    IBOutlet UITabBarController *tabBarController;

    IBOutlet UIButton *UploadButton;
    IBOutlet UIButton *ConvertorButton;
    IBOutlet UIButton *CompletedButton;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

*Viewcontroller.m:

@implementation StatusViewController
@synthesize tabBarController ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createTabView];
}
-(void)createTabView
{
    ....

    ....
    [ConvertorButton addSubview:Label3];

    [CompletedButton setTag:3];


    [CompletedButton setImage:[UIImage imageNamed:@"Overlay-2.png"] forState:UIControlStateNormal];
    [CompletedButton setImage:[UIImage imageNamed:@"background.jpg"] forState:UIControlStateSelected];
    [CompletedButton setImage:[UIImage imageNamed:@"background.jpg"] forState:UIControlStateHighlighted];

    //[[CompletedButton layer] setBorderWidth:2.0f];
    // [[CompletedButton layer] setBorderColor:[UIColor grayColor].CGColor];

    UILabel *Label4=[[UILabel alloc]initWithFrame:CGRectMake(6, 8, 70, 30)];
    [Label4 setTextAlignment:UITextAlignmentCenter];
    [Label4 setText:@"Completed"];


    Label4.backgroundColor=[UIColor clearColor];
    [Label4 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
    Label4.textColor=[UIColor whiteColor];
    [CompletedButton addSubview:Label4];

    // [self selectTab:1];

    [self.view addSubview:UploadButton];

    [self.view addSubview:ConvertorButton];

    [self.view addSubview:CompletedButton];


}

Thanks for your help

like image 722
user2656381 Avatar asked Aug 09 '13 08:08

user2656381


People also ask

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

How do I add items to my tab bar?

Add a new Tab Bar ItemDrag View controller into the storyboard. Drag Tab Bar Item into the newly created view controller, it will sit at the same level of the View .


2 Answers

If You want to use UITabBarController in your UIViewController class then use this below code...

UIViewController .h Class -

    @property (nonatomic, retain) UITabBarController *tab;

UIViewController .m Class -

Add this in ViewDidLoad method...

    self.tab=[[UITabBarController alloc]init];

    // FirstViewController
    First *fvc=[[First alloc]initWithNibName:nil bundle:nil];
    fvc.title=@"First";
    fvc.tabBarItem.image=[UIImage imageNamed:@"i.png"];

    //SecondViewController
    Second *svc=[[Second alloc]initWithNibName:nil bundle:nil];
    svc.title=@"Second";
    svc.tabBarItem.image=[UIImage imageNamed:@"im.png"];

    //ThirdViewController
    Third *tvc=[[Third alloc]initWithNibName:nil bundle:nil];
    tvc.title=@"Third";
    tvc.tabBarItem.image=[UIImage imageNamed:@"img.png"];

    self.tab.viewControllers=[NSArray arrayWithObjects:fvc, svc, tvc, nil];

    [self.view addSubview:self.tab.view];

here First, Second and Third are three different UIViewControllers. And you don't need to give the action on Tabs.

It will work...

like image 149
Anand Gautam Avatar answered Dec 17 '22 14:12

Anand Gautam


If you're are starter,best practice is to search in google for the latest api samples,then understand the code and make your own world.

1) You can find the related sample codes from apple here.

2) TweetieBar --Here is the sample code with custom TabBarController(TweetieTabBar)

like image 30
Master Stroke Avatar answered Dec 17 '22 15:12

Master Stroke