自定义UINavigationBar背景颜色

时间:2014-06-23 02:58:51

标签: ios objective-c uinavigationcontroller uinavigationbar background-color

enter image description here

以下是UINavigationController,其中包含自定义UINavigationBar (我将其高度添加到额外的12px)

CustomNavigationBar

- (void)initialize
{
    [self setTransform:CGAffineTransformMakeTranslation(0, -12)];
}

- (id)initWithCoder:(NSCoder *)aDecoder {

    if ((self = [super initWithCoder:aDecoder])) {
        [self initialize];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        [self initialize];
    }
    return self;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    return CGSizeMake(320, 44 + 12);
}

- (void)layoutSubviews {
    [super layoutSubviews];

    NSArray *classNamesToReposition = @[@"_UINavigationBarBackground"];

    for (UIView *view in [self subviews]) {

        if ([classNamesToReposition containsObject:NSStringFromClass([view class])]) {

            CGRect bounds = [self bounds];
            CGRect frame = [view frame];
            frame.origin.y = bounds.origin.y + 12;
            // hack for iOS 6
            if (IOS7_ABOVE) frame.origin.y -= 20.0f;
            frame.size.height = bounds.size.height + 20.f;

            [view setFrame:frame];
        }
    }
}

在第二个viewController中,我隐藏了navigationBar中的viewWillappear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // without this line, the bottom part will be black color
    self.navigationController.view.backgroundColor = barBackgroundColor;

    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    [[UINavigationBar appearance] setBarTintColor:barBackgroundColor];
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    ...
}

我的问题是为什么我将相同的backgroundColor设置为[[UINavigationBar appearance] setBarTintColor:barBackgroundColor];,但它显示的颜色不同,还有一些颜色在两种颜色之间。

这是什么问题?对此有何解决方案?

0 个答案:

没有答案