您是否注意到在创建UITableView时不再设置UINavigationBar,即使在给它标题或按钮后也是如此?
现在我对如何在我的UITableView上放置导航栏感到很生气。这似乎真的不可能。我试图用导航栏添加到我的tableView子视图,但似乎毫无价值,因为当我向下滚动时,导航栏向下滚动,它不应该向下滚动。
关于如何实施它的任何想法?
修改
好吧,我一如既往地继续使用File - >新 - >文件.. - > UITableView的。然后我设置了一些代码,当我写了
self.navigationItem.title = @"MyTitle";
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
并尝试在Simulator上测试,没有出现导航栏。
我的初始代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"TabTitle";
self.tabBarItem.image = [UIImage imageNamed:@"img.png"];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
}
return self;
}
我无法解释为什么它不再出现。我还尝试创建一个新项目,并从导航栏出现的项目中导入我的类,但同样也是如此。
EDIT2 *
该应用是一个基于tabBased的应用程序。 以下是用于设置tabBar的App委托代码。
UIViewController *viewController1, *viewController4;
UITableViewController *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:@"First_iPhone" bundle:nil];
viewController2 = [[Tips alloc] initWithNibName:@"Table" bundle:nil];
viewController3 = [[Favorites alloc] initWithNibName:@"Test_iPhone" bundle:nil];
viewController4 = [[SecondViewController alloc] initWithNibName:@"Second_iPhone" bundle:nil];
}
答案 0 :(得分:1)
您正在发起UITabBarController
并将4 UIViewControllers
设置为相应的UITabbarViewControllers
。由于其中两个是正常UIViewControntroller
而另外两个是UITableViewController
,因此无法使用导航栏。您必须加载viewController,您希望导航栏形成UINavigationController
。正确的方法是(假设vc3是你想要导航栏的那个):
UIViewController *viewController1, *viewController4;
UITableViewController *viewController2, viewController3;
UINavigationController *vc3NavController;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:@"First_iPhone" bundle:nil];
viewController2 = [[Tips alloc] initWithNibName:@"Table" bundle:nil];
viewController3 = [[Favorites alloc] initWithNibName:@"Test_iPhone" bundle:nil];
vc3NavController = [[UINavigationController alloc] initWithRootViewController:viewController3];
viewController4 = [[SecondViewController alloc] initWithNibName:@"Second_iPhone" bundle:nil];
}
然后加载vc3NavController而不是viewController3作为相应的选项卡。 所以你有:UITabBarController - > UINavigationController - > YourViewController
也许Creating a Navigation Interface也可以帮到你。