ios tabBarControl,navigationcontroller标题和按钮编辑

时间:2014-05-06 18:41:49

标签: ios uitabbarcontroller uinavigationbar

我一直在我的应用程序中使用标准的TabBarController和顶级NavigationBar。 tabBar在appDelegate中加载,初始化navigationBar以使用图像而不是中心标题,这对于所有四个tabBar视图都存在。偶尔我会将一个新视图推送到视图控制器上,并且运行良好。

我想要做的是能够在打开时更改4个tabBar视图控制器中的每一个的NavigationBar。我在做这件事时遇到了麻烦。我的初始navigationBar有一个图像,加载到appDelegate中。我的问题是:

  • 每个viewController是否应该根据需要在相应的viewWillAppear方法中从头开始创建一个新的navigationBar?
  • 应该编辑什么是navigationController和navigationBar--总是appDelegate的navigationController,tabBarController.navigationController,或者只是每个视图中的self.navigationController? (一般来说,大多数编辑大多数都不适用于我,导致不会发生任何变化)
  • 如果我使用imageView覆盖标准标题(通常是tabBar的当前视图的标题),并希望另一个tabBar视图使用标准标题,我应该如何删除titleView图像,并重置回文本?而ViceVersa,依赖于观点?这就是我真正想要取得的成功。

我想我正在寻找管理每个视图的navigationBar的标准做法,并根据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];

1 个答案:

答案 0 :(得分:2)

这可能是你需要的:

- (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:@"...."];
}