ios tabBarControl、navigationcontroller标题和按钮编辑

ios tabBarControl、navigationcontroller标题和按钮编辑,ios,uitabbarcontroller,uinavigationbar,Ios,Uitabbarcontroller,Uinavigationbar,我一直在应用程序中使用标准的TabBarController和顶部导航栏。选项卡栏加载到appDelegate中,初始化导航栏以使用图像而不是中心标题,这适用于所有四个选项卡栏视图。偶尔我会将一个新视图推送到视图控制器上,这样就足够了 我想做的是,当4个选项卡栏视图控制器打开时,能够更改它们的导航栏。我做这件事有困难。我的初始导航栏有一个图像,加载在appDelegate中。我的问题是: 每个viewController是否应该根据需要,在各自的ViewWillAspect方法中从头开始创建新

我一直在应用程序中使用标准的TabBarController和顶部导航栏。选项卡栏加载到appDelegate中,初始化导航栏以使用图像而不是中心标题,这适用于所有四个选项卡栏视图。偶尔我会将一个新视图推送到视图控制器上,这样就足够了

我想做的是,当4个选项卡栏视图控制器打开时,能够更改它们的导航栏。我做这件事有困难。我的初始导航栏有一个图像,加载在appDelegate中。我的问题是:

  • 每个viewController是否应该根据需要,在各自的ViewWillAspect方法中从头开始创建新的导航栏
  • 应该编辑哪些navigationController和navigationBar—始终是每个视图中appDelegate的navigationController、tabBarController.navigationController,或者只是self.navigationController?(通常情况下,大多数编辑都不适用于我,不会导致任何更改)
  • 如果我用imageView替代标准标题(通常是选项卡栏当前视图的标题),并希望另一个选项卡栏视图使用标准标题,我应该如何删除标题视图图像,并重置回文本?维切维萨呢,看情况而定?这就是我真正想要实现的,但没有成功
我想我正在寻找管理每个视图的导航栏以及每个tabBarItem更改导航栏的标准实践

// in appDelegate
- (void)initNav {
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;
    self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
                                             self.secondViewController,
                                             self.thirdViewController,
                                             self.forthViewController,
                                             nil];


    [self.window addSubview:self.tabBarController.view];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    self.navigationController.navigationBarHidden = NO;

    // customize background color of nav bar to "blue"ish color
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];

    [self createNavs];
}

// also in appDelegate
- (void)createNavs {
    // white text when present
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                UITextAttributeTextColor,
                                [UIColor clearColor],
                                UITextAttributeTextShadowColor, nil];

    [[UIBarButtonItem appearance] setTitleTextAttributes: attributes
                                                forState: UIControlStateNormal];


    AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    [self.navigationController.navigationBar setTranslucent:NO];

    // setup left button (currently unused -- no left button)
    /*
    /*
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"glyphicons_049_star.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchDown];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    */

    // setup logo in center
    self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"synced_top_logo.png"]];

    // setup right button (currently unused -- no right button)
    /*
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
    rightButton.title = @"edit";
    [rightButton setTarget:self ];
    [rightButton setAction:@selector(editClick:)];
    self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
    */

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
}
编辑: 这是下面发布的解决方案的简化版本,非常有用,并允许进行所需的定制。每个视图控制器独立于此自定义其导航栏。从一开始就应该这样做

tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController], 
                                         [[UINavigationController alloc] initWithRootViewController:self.secondViewController],
                                         [[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
                                         [[UINavigationController alloc] initWithRootViewController:self.forthViewController],
                                         nil];

这可能是您需要的:

- (void)initTabbar {

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;

    /*
     You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
    */

    //You don't need to hang on to this becuase the proceeding UINavigationController will handle it
    FirstViewController *firstViewController = [[FirstViewController alloc] ...];

    //You'll need to declare this in your header
    self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];


    //Second view allocation
    SecondViewController *secondViewController = [[SecondViewController alloc] ...];
    self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];

    //Third view allocation
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
    self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];


    //Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
                                             self.secondNavigationController,
                                             self.thirdNavigationController,
                                             nil];


    [self.window addSubview:self.tabBarController.view];

    [self createNavs];
}

//This is more of a 'formatNavs' now
- (void)createNavs {

    //Now you can customize each of the UINavigationController's UINavigationBars seperatly
    self.firstNavigationController.navigationBar.backgroundColor    = [UIColor colorWithHexString:@"00A3E1"];
    self.firstNavigationController.navigationBar.tintColor          = [UIColor colorWithHexString:@"00A3E1"];
    self.firstNavigationController.navigationBar.barTintColor       = [UIColor colorWithHexString:@"00A3E1"];

    self.secondNavigationController.navigationBar.backgroundColor   = [UIColor colorWithHexString:@"...."];
    self.secondNavigationController.navigationBar.tintColor         = [UIColor colorWithHexString:@"...."];
    self.secondNavigationController.navigationBar.barTintColor      = [UIColor colorWithHexString:@"...."];

    self.thirdNavigationController.navigationBar.backgroundColor    = [UIColor colorWithHexString:@"...."];
    self.thirdNavigationController.navigationBar.tintColor          = [UIColor colorWithHexString:@"...."];
    self.thirdNavigationController.navigationBar.barTintColor       = [UIColor colorWithHexString:@"...."];
}

让我明白这一点。您有一个
uitabarcontroller
包装在
UINavigationController
中?因此,在显示
UITabBarController
的第一个视图中,当您按下新的
UIViewController
时,是否希望
UITabBarController
选项卡栏消失?我这样问是因为这不是使用
UITabBarController
时的惯例。您不应该将
uitabarcontroller
包装在
UINavigationController
@Miro中,这只是一个猜测,但您是否尝试过这样做cread@random,这似乎就是正在发生的事情。我相信我最初这么做是因为导航栏最初是100%静态的。现在它已经到了极限。最好的方法是什么,为每个添加到TabBarController的ViewController添加一个NavigationController?他们将需要独特的标题+按钮,现在,一些将推动其他视图。非常好。我在上面添加了一个编辑,它简化了相同的逻辑,但相同的想法是每个选项卡都有自己的navigationController堆栈。