我一直在关注如何以编程方式添加UITabBarController的不同教程.这很容易使用故事板来实现,但是因为我正在尝试以编程方式学习如何做事我无法做到这一点.目前我已经有了这个代码didFinishLaunchingWithOptions.
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *tabs = [[NSMutableArray alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[MenuViewController alloc] init]];
[tabBarController setViewControllers:tabs];
[tabs addObject:navController];
[self.window addSubview:tabBarController.view];
Run Code Online (Sandbox Code Playgroud)
编辑代码:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
这对于我的rootViewController调用MenuViewController没有任何意义.我怎样才能实现这一目标?
我正在尝试以编程方式制作 UITabBarController 。这似乎有效,我有 2 个选项卡,问题是第一次加载时 RootViewController 中没有显示第二个选项卡标题。怎么会?
这是加载 RootViewController 的时候:

当我单击没有标题的第二个选项卡时,会出现标题:

当我返回时,第二个选项卡标题仍然存在:

我的问题是为什么加载 RootViewController 时它没有显示。
我的 appDelegate 方法在 didFinishLaunchingWithOptions 中:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:@"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController, navController2];
[self.window setRootViewController:tabBarController];
Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式将自定义UILabel中心放入UITableViewCell.问题是它似乎没有中心.我正在使用此代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[chatCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
float yPos = (CGRectGetHeight(cell.contentView.frame) - CGRectGetHeight(cell.homeTeamLabel.frame)) / 2;
cell.homeTeamLabel = [[UILabel alloc] initWithFrame:CGRectMake(22.0, yPos, 220.0, 15.0)];
cell.homeTeamLabel.text = [items objectAtIndex:indexPath.row];
cell.homeTeamLabel.font = [UIFont systemFontOfSize:14.0];
cell.homeTeamLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.homeTeamLabel];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这是一张它看起来如何的图片.你可以看到它没有居中.我怎么能把它集中起来?
