使用image和tintColor自定义UIBarButtonItem

时间:2013-12-06 16:05:29

标签: uibarbuttonitem tintcolor

我的导航栏有三个组件:后退按钮,磁贴和其他自定义按钮。

我的目标是能够为该按钮添加自定义图像,但仍然遵循色调颜色逻辑。

在我设置的AppDelegate.m didFinishLaunchingWithOptions方法中:

ViewController *welcomeVC = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]
                                                initWithRootViewController:welcomeVC];
navigationController.navigationBar.tintColor = [UIColor orangeColor];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, nil]];

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

然后在ViewController.m中我有:

- (void)viewWillAppear:(BOOL)animated
{
    [self setUpImageBackButton];
}

- (void)setUpImageBackButton {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(popCurrentViewController)];
}

-(IBAction)nextButtonTapped:(id)sender {
//create the second view
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
    secondController.title = @"SecondViewController";
    [self.navigationController pushViewController:secondController animated:YES];
}

-(void)popCurrentViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

在上一个视图控制器中我有:

- (void)viewWillAppear:(BOOL)animated
{
    [self setUpImageBackButton];
}

- (void)setUpImageBackButton {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(popCurrentViewController)];

    UIButton *settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [settingsButton setTitle:@"\u2699" forState:(UIControlStateNormal)];
    [settingsButton.titleLabel setFont:[UIFont systemFontOfSize:30]];
    [settingsButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingsButton];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor orangeColor];
}

-(IBAction)nextButtonTapped:(id)sender {
    //create the third view
    ThirdViewController *thirdController = [[ThirdViewController alloc] initWithNibName:nil bundle:NULL];
    thirdController.title = @"ThirdViewController";
    [self.navigationController pushViewController:thirdController animated:YES];
}

-(void)popCurrentViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

我的问题是:

  1. 我可以为导航栏按钮使用自定义图像,但仍然具有tintColor效果吗?如果是这样,有人可以发布代码示例如何做到这一点。
  2. 为什么我的设置按钮不遵守色调颜色规则,因为它保持白色。信息按钮和后退按钮都是橙色的,因为我在AppDelegate调用中设置它们。

0 个答案:

没有答案