如何在UINavigationBar上设置背景颜色(tintColor)

时间:2012-11-27 09:09:18

标签: iphone xcode uinavigationbar tintcolor

我希望你能帮我设置导航栏的背景颜色。正如您在下面的第一张图片中看到的那样,条形图是浅蓝色/灰色,但我希望它像第二张图片“收藏夹”。

enter image description here

我的项目中有另一个viewController(参见第二张图片“收藏夹”)。但我看不出我在这里做错了什么,而且navigationBar只会采用浅蓝/灰色而不是黄色。

enter image description here

我的代码是这样的:

------------------ InstantCabAppDelegate.m文件----------- START -------

//InstantCabAppDelegate.m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Create tabBarController instance
    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    // Create instances of navigation controller and view controllers
    orderController = [[OrderController2 alloc] initWithNibName:@"Order2" bundle:nil];

    UINavigationController* firstnavigationController = [[UINavigationController alloc] initWithRootViewController:orderController];

    ICStatusViewController *statusController = [[ICStatusViewController alloc] initWithStatus];
    UINavigationController *statusNavController = [[UINavigationController alloc] initWithRootViewController:statusController];

    ICSettingsViewController *settingsController = [[ICSettingsViewController alloc] init];
    UINavigationController *settingsNavController = [[UINavigationController alloc] initWithRootViewController:settingsController];

    UIViewController *company_controller = nil;
    company_controller = [[ICCompaniesGridController alloc] initWithCompanies:[[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Companies" ofType:@"plist"]]];   

    tabBarController.viewControllers = [NSArray arrayWithObjects:firstnavigationController, settingsNavController, statusNavController, company_controller, nil];

    [window setRootViewController:tabBarController];
    [window makeKeyAndVisible];
    return YES;
}

------------------ ICStatusViewController.m文件----------- START -------

//  ICStatusViewController.m file

#import "ICStatusViewController.h"
#import "LocalizationSystem.h"
#import "ICTemporaryStore.h"
#import "Debug.h"

@implementation ICStatusViewController

@synthesize doneCallbackBlock=_doneCallbackBlock, cancelCallbackBlock=_cancelCallbackBlock;
@synthesize myTableView, no_statuses, no_statuses_label, delegate;

- (id)initWithStatus
{
    if ((self = [super initWithNibName:@"StatusList" bundle:nil])) 
    {
        self.title = AMLocalizedString(@"kStatusTitle", @"");
        self.tabBarItem.image = [UIImage imageNamed:@"status_icon"];

        self.no_statuses = AMLocalizedString(@"kStatusListNoStatusList", @"");
        first_color = TABLE_VIEW_BACKGROUND_COLOR;
        second_color = first_color;
        self.doneCallbackBlock = nil;
        self.cancelCallbackBlock = nil;
    }
    return self;
}

- (void)viewDidLoad 
{  
    [super viewDidLoad];

    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background-nologo"]];
    self.view.backgroundColor = [UIColor clearColor];
    self.myTableView.backgroundColor = TABLE_VIEW_BACKGROUND_COLOR;

    if ([statusObjects count] == 0)
    {
        self.myTableView.hidden = YES;

        self.no_statuses_label.text = self.no_statuses;
        self.no_statuses_label.hidden = NO;
        no_status_background.hidden = NO;
    }

    [self.no_statuses_label setTextColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if ([statusObjects count] == 0)
    {
        self.myTableView.hidden = YES;
        self.no_statuses_label.text = self.no_statuses;
        self.no_statuses_label.hidden = NO;
        no_status_background.hidden = NO;
        self.navigationItem.rightBarButtonItem = nil;
    }
    else
    {
        self.myTableView.hidden = NO;
        self.no_statuses_label.hidden = YES;
        no_status_background.hidden = YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger retval = 0;
    for (int i=0; i<[statusObjects count]; i++)
    {
        StatusObj *add = [statusObjects getStatusAtIndex:i];
        if (add.status_id && [add.status_id length] > 0 && [add.status_type isEqualToString:@"STATUS_ORDER"]) 
        {
            retval++;
        }
    }
    return retval;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifierStatus = @"StatusCell";
    __unsafe_unretained NSString *CellIdentifier = CellIdentifierStatus;
    StatusCell* cell = (StatusCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[StatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [[cell textLabel] setTextColor:TABLE_VIEW_TEXT_LABEL_COLOR];
        [[cell detailTextLabel] setTextColor:TABLE_VIEW_DETAIL_TEXT_LABEL_COLOR];
        [[cell detailTextLabel] setNumberOfLines:3];
    }    

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    dateString = [dateFormat stringFromDate:current_order.order.time];
    NSString *str = AMLocalizedString(@"kStatusListDetailsToDay", @"");
    textLabelString = [NSString stringWithFormat:@"%@ - %@", str, dateString];

    [cell.textLabel setText:textLabelString];

    [cell.detailTextLabel setText:detailTextString];

    [cell setBackgroundColor:[UIColor redColor]];

    [cell.dateLabel setBackgroundColor:[UIColor clearColor]];
    cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

------------------ ICStatusViewController.h文件----------- START -------

//  ICStatusViewController.h

#import <UIKit/UIKit.h>
#import "Status.h"
#import "StatusCell.h"
#import "StatusControllerDelegate.h"
#import "ICStatusDetailsController.h"

typedef void (^ICStatusViewControllerDoneCallbackBlock)(id userInfo);
typedef void (^ICStatusViewControllerCancelCallbackBlock)(void);

@interface ICStatusViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate, ICStatusDetailsControllerDelegate> {
    id<Statuses> statusObjects;
    NSString *no_statuses;
    NSOperationQueue *queue;
    NSTimer *statusTimer;
    __unsafe_unretained id<StatusControllerDelegate> delegate;

    UIColor *first_color;
    UIColor *second_color;

    IBOutlet __unsafe_unretained UITableView *myTableView;
    IBOutlet __unsafe_unretained UILabel *no_statuses_label;
    IBOutlet __unsafe_unretained UIImageView *no_status_background;

@private
    ICStatusViewControllerDoneCallbackBlock _doneCallbackBlock;
    ICStatusViewControllerCancelCallbackBlock _cancelCallbackBlock;
}

@property (nonatomic, unsafe_unretained) UITableView* myTableView;
@property (nonatomic, copy) NSString* no_statuses;
@property (nonatomic, unsafe_unretained) UILabel* no_statuses_label;
@property (nonatomic, unsafe_unretained) id <StatusControllerDelegate> delegate;
@property (nonatomic, copy) ICStatusViewControllerDoneCallbackBlock doneCallbackBlock;
@property (nonatomic, copy) ICStatusViewControllerCancelCallbackBlock cancelCallbackBlock;

- (id)initWithStatus;

@end

4 个答案:

答案 0 :(得分:2)

您可以设置导航栏的色调颜色或使用IMAGE而不是导航栏并隐藏导航栏,使用:

navigationController.navigationBarHidden = YES;

答案 1 :(得分:1)

您不是在谈论UITableView,而是关于顶部的UINavigationBar。我修正了你的问题以反映这一点。基本上有两种方法可以设置tintColor

更简单的选项将使用外观协议(iOS 5及更高版本),因为这会更改整个应用中的每个NavigationBar:
[[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];

或者您对每UINavigationBarUINavigationController执行此操作 [navigationController.navigationBar setTintColor:[UIColor yellowColor]];

答案 2 :(得分:0)

只需在viewDidLoad:类的ICStatusViewController方法中将此行设置黄色设置为navigationcontroller。

[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];

还有像贝娄这样的图像..

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"yourImageName"] forBarMetrics:UIBarMetricsDefault];// write your image name like yellow image 

我希望这可以帮助你...

答案 3 :(得分:0)

你的tableView的风格是什么?如果tableView的样式为UITableViewStyleGrouped,则无法在iOS 6上更改backgroundColor。尝试更改tableView的backgroundView。

self.settingsTableView = [[UITableView alloc] initWithFrame:rect
                                                      style:UITableViewStyleGrouped];
UIView *view = [[UIView alloc] initWithFrame:settingsTableView.frame];
view.backgroundColor = [UIColor redColor];
settingsTableView.backgroundView = view;
相关问题