我只需要在某些特定視圖上更改導航欄的顏色。 關於修改導航欄顏色的討論很多,比如https://stackoverflow.com/a/18870519/938380,但它們都改變了同一導航層次結構下每個視圖的導航欄顏色。如何更改特定視圖上導航欄的顏色
我想改變具體意見顏色,並保持其他意見相同的顏色。我如何實現這一目標?
我只需要在某些特定視圖上更改導航欄的顏色。 關於修改導航欄顏色的討論很多,比如https://stackoverflow.com/a/18870519/938380,但它們都改變了同一導航層次結構下每個視圖的導航欄顏色。如何更改特定視圖上導航欄的顏色
我想改變具體意見顏色,並保持其他意見相同的顏色。我如何實現這一目標?
如果您使用標準導航欄,則無法執行此操作。但是你可以使用一些作弊;)例如,您可以將此代碼(或像這樣)添加到您的控制器:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
oldColor = self.navigationController.navigationBar.backgroundColor;//probably barTintColor instead of backgroundColor
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.backgroundColor = oldColor;
}
可以設置酒吧色調物業navigationController
我希望這個幫你
[self.navigationController.navigationBar setBarTintColor:[的UIColor redColor];
我通過一個擴展的UIViewController處理這種事情。在我的情況下,我想讓選定的導航欄透明。你可以擴展這個來處理顏色。這樣可以將相同的代碼粘貼到多個控制器中。下面的代碼。
從viewWillAppear
你叫[self makeNavigationBarTransparent]
。從viewWillDisappear
撥打電話[self restoreNavigationBar]
對於您的情況,您只需將其擴展爲makeNavigationBarColored
即可。
一個選項也是子類UINavigationController來創建一個具有特定顏色的類。在它的實現中使用這個擴展來設置顏色。在你的故事板中,你想要這種顏色的任何控制器都是你的新類類型。然後,你正在從故事板做這一切。
的UIViewController + TransparentNavigationBar.h
#import <UIKit/UIKit.h>
@interface UIViewController (TransparentNavigationBar)
/** Makes the current navigation bar transparent and returns a context holding
* the original settings.
*/
- (void) makeNavigationBarTransparent;
/**
* Restores the current navigation bar to its original settings.
*/
- (void) restoreNavigationBar;
@end
的UIViewController + TransparentNavigationController.m
#import "UIViewController+TransparentNavigationBar.h"
#import <objc/runtime.h>
@interface VSSNavigationBarContext:NSObject
/** Backup of nav bar image used to restore on push. */
@property UIImage *originalNavBarBackgroundImage;
/** Backup of nav bar shadow image used to restore on push. */
@property UIImage *originalNavBarShadowImage;
/** Backup of nav bar color used to restore on push. */
@property UIColor *originalNavBarColour;
@end
@implementation VSSNavigationBarContext
- (id) init {
self=[super init];
if (self){
self.originalNavBarBackgroundImage=nil;
self.originalNavBarShadowImage=nil;
self.originalNavBarColour=nil;
}
return self;
}
@end
static char const * const ObjectTagKey = "NavBarContextTag";
@implementation UIViewController (TransparentNavigationBar)
- (VSSNavigationBarContext *) getContext
{
VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
return context;
}
- (void) makeNavigationBarTransparent{
VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
if (context == nil){
context=[[VSSNavigationBarContext alloc] init];
context.originalNavBarBackgroundImage=[self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
context.originalNavBarShadowImage=self.navigationController.navigationBar.shadowImage;
context.originalNavBarColour=self.navigationController.view.backgroundColor;
// Store the original settings
objc_setAssociatedObject(self, &ObjectTagKey, context, OBJC_ASSOCIATION_RETAIN);
}
//
// Make transparent
//
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){
self.navigationController.navigationBar.translucent = YES;
}
else{
self.navigationController.navigationBar.translucent = NO;
}
self.navigationController.view.backgroundColor = [UIColor clearColor];
}
- (void) restoreNavigationBar
{
VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
if (context != nil){
// Restore original
[self.navigationController.navigationBar setBackgroundImage:context.originalNavBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = context.originalNavBarShadowImage;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){
self.navigationController.navigationBar.translucent = YES;
}
else{
self.navigationController.navigationBar.translucent = NO;
}
self.navigationController.view.backgroundColor = context.originalNavBarColour;
}
}
@end
非常甜蜜,非常感謝! – Cesare 2017-03-08 20:02:06