我正在一个聊天项目中,必须在其中使用UITabBar
。我在此项目中使用情节提要,但根据要求,我在项目中添加了自定义TabBar,以将其安装在视图顶部。
现在的问题是Custom TabBar在视图顶部显示良好,但是当我单击选项卡时,它仅显示黑屏而不显示ViewController。
我正在共享我的代码,请为我提出解决方案。
我的.h文件
@interface TabBar : UITabBarController
{
}
@property(strong,nonatomic) UITabBarController *tabbarcontroller;
@property(strong,nonatomic) ChatListVC *chatlist;
@property(strong,nonatomic) ContactListVc *contactlist;
@property(strong,nonatomic) FindfriendsVC *findfriends;
@end
我的.m文件
@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"]];
}
输出的快照是
最佳答案
看来您的视图控制器未正确初始化。
如果视图控制器全部在情节提要中,请尝试替换
_chatlist = [ChatListVC new];
_contactlist = [ContactListVc new];
_findfriends = [FindfriendsVC new];
与:
_chatlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChatListIdentifier"];
_contactlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListIdentifier"];
_findfriends = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"FindfriendsIdentifier"];
其中标识符字符串是您在情节提要中设置的自己的标识符。