【问题标题】:Howto set backgroundColor (tintColor) on UINavigationBar如何在导航栏上设置背景颜色(色调颜色)
【发布时间】:2012-11-27 09:09:17
【问题描述】:

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

我的项目中有另一个 viewController(参见第二张图片“Favoritter”),它可以工作。但是我看不到我在这里做错了什么,并且导航栏只会采用这种浅蓝色/灰色而不是黄色。

我的代码是这样的:

------------------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

【问题讨论】:

    标签: iphone xcode uinavigationbar tintcolor


    【解决方案1】:

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

    navigationController.navigationBarHidden = YES;
    

    【讨论】:

      【解决方案2】:

      您说的不是UITableView,而是顶部的UINavigationBar。我解决了你的问题以反映这一点。基本上有两种方法可以设置它的tintColor

      更简单的选项是使用外观协议(iOS 5 及更高版本),因为这会改变整个应用程序中的每个 NavigationBar:
      @987654324 @

      或者您对每个UINavigationController 中的每个UINavigationBar 都执行此操作。
      [navigationController.navigationBar setTintColor:[UIColor yellowColor]];

      【讨论】:

        【解决方案3】:

        只需在ICStatusViewController 类的viewDidLoad: 方法中添加此行以将黄色设置为导航控制器..

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

        还有像下面这样的图像..

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

        希望对你有帮助……

        【讨论】:

        • 感谢它的工作。我按照您告诉我的方式将这一行放在“viewDidLoad”中 [self.navigationController.navigationBar setTintColor:NAVIGATION_BAR_TINT_COLOR];
        • @user1758654 欢迎朋友,如果这个答案对你有用,请接受并投票给答案。.. :)
        【解决方案4】:

        你的 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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-12
          • 1970-01-01
          • 2015-05-14
          • 1970-01-01
          • 1970-01-01
          • 2011-12-05
          • 1970-01-01
          相关资源
          最近更新 更多