Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

IOS: Adding viewcontroller to custom TabBar in ios

i am working on a chat project in which i have to use a UITabBar. I am using storyboard in this project but according to requirement i added custom TabBar in project to mount it on the top of the view. Now the problem is Custom TabBar is showing well on the top of view but when i click on the tabs it only shows black screen instead of showing the ViewControllers. i am sharing my code please suggest me the solution of it.

my .h file

@interface TabBar : UITabBarController
{

}
@property(strong,nonatomic) UITabBarController *tabbarcontroller;
@property(strong,nonatomic) ChatListVC *chatlist;
@property(strong,nonatomic) ContactListVc *contactlist;
@property(strong,nonatomic) FindfriendsVC *findfriends;

@end

my .m file

@implementation TabBar

    - (void)viewDidLoad

        {
            [super viewDidLoad];
            // Do any additional setup after loading the view.
        //    self.navigationItem.hidesBackButton=YES;

            self.title = @"Chat Home Page";

            self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];

            _chatlist = [ChatListVC new];
            _contactlist = [ContactListVc new];
            _findfriends = [FindfriendsVC new];     //initWithNibName:@"FindfriendsVC" bundle:nil];//[FindfriendsVC new];

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

            self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];

            //self.navigationController.navigationBar.frame.size.height;
            //UITabBar *tabBar = self.tabBarController.tabBar;
            //CGFloat topBarOffset = self.topLayoutGuide.length;
            self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);

            UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
            UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
            UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];

            item0.title = @"Chat List";
            item1.title = @"Contacts";
            item2.title = @"Find Friends";

            [item0 setFinishedSelectedImage:[UIImage imageNamed:@"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"chatlist.png"]];
            [item1 setFinishedSelectedImage:[UIImage imageNamed:@"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"contacts.png"]];
            [item2 setFinishedSelectedImage:[UIImage imageNamed:@"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"findfriends.png"]];

        }

snapshot of the output is enter image description here

Upvotes: 0

Views: 919

Answers (1)

Allanah Fowler
Allanah Fowler

Reputation: 1281

It looks like your view controllers aren't being initialised properly.

If your view controllers are all in the Storyboard, try replacing

_chatlist = [ChatListVC new];
_contactlist = [ContactListVc new];
_findfriends = [FindfriendsVC new];

with:

_chatlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChatListIdentifier"];
_contactlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListIdentifier"];
_findfriends = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"FindfriendsIdentifier"];

Where the identifier string is your own identifier that you have set up in the Storyboard.

Upvotes: 2

Related Questions