IOS: 添加 viewcontroller 到 ios 中的自定义 TabBar
IOS: Adding viewcontroller to custom TabBar in ios
我正在做一个聊天项目,我必须在其中使用 UITabBar
。我在这个项目中使用故事板,但根据要求,我在项目中添加了自定义 TabBar 以将其安装在视图顶部。
现在的问题是自定义 TabBar 在视图顶部显示良好,但是当我单击选项卡时它只显示黑屏而不是显示 ViewControllers。
我正在分享我的代码,请给我建议解决方案。
我的.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"];
其中标识符字符串是您在 Storyboard 中设置的自己的标识符。
我正在做一个聊天项目,我必须在其中使用 UITabBar
。我在这个项目中使用故事板,但根据要求,我在项目中添加了自定义 TabBar 以将其安装在视图顶部。
现在的问题是自定义 TabBar 在视图顶部显示良好,但是当我单击选项卡时它只显示黑屏而不是显示 ViewControllers。
我正在分享我的代码,请给我建议解决方案。
我的.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"];
其中标识符字符串是您在 Storyboard 中设置的自己的标识符。