UINavigationBar的简单实用
程序员文章站
2022-03-26 19:08:52
...
==================navi bar 是否是半透明======================
self.navigationController.navigationBar.translucent = NO;
==================黑色背景,白色状态栏======================
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
=================白色背景,黑色状态栏======================
//白色背景,黑色状态栏
[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
====================bar的tintColor=========================
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
=====================设置title颜色和大小==================
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor orangeColor], NSForegroundColorAttributeName,
[UIFont boldSystemFontOfSize:22], NSFontAttributeName,nil]
];
====================设置返回键图标==================
UIImage *image = [UIImage imageNamed:@"icon_back_black"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorImage = image
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = image;
======================设置bar的底线===================
//设置bar的底线
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[self imageWithColor:[UIColor redColor]]];
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"barShadow"]];
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Example
#import <UIKit/UIKit.h>
@interface UIViewController (WNNavigationBar)
//不传bartintcolor的情况下和默认情况下 是 default。也就是状态栏为黑色字体
@property (nonatomic,assign) BOOL isDefaultBarStyle;
//设置tintColor
@property (nonatomic,strong) UIColor *wnBarTintColor;
@end
//
// UIViewController+WNNavigationBar.m
// danche
//
// Created by WNRS on 2017/6/12.
// Copyright © 2017年 wuna. All rights reserved.
//
#import "UIViewController+WNNavigationBar.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
@implementation UIViewController (WNNavigationBar)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(viewWillAppear:);
SEL swizzledSelector = @selector(wn_viewWillAppear:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)wn_viewWillAppear:(BOOL)animated
{
// Forward to primary implementation.
[self wn_viewWillAppear:animated];
if (self.isDefaultBarStyle) { //白色navigationbar
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:18], NSFontAttributeName,nil]];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
self.navigationController.navigationBar.translucent = NO;
//--
UIImage *image = [UIImage imageNamed:@"icon_back_black"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorImage = image;
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = image;
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"barShadow"]];
}else{ //黑色navigationbar
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.navigationBar setBarTintColor:self.wnBarTintColor?self.wnBarTintColor:HexRGB(0x3A3A3A)];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:18], NSFontAttributeName,nil]];
[self.navigationController.navigationBar setTintColor:HexRGB(0x3A3A3A)];
self.navigationController.navigationBar.translucent = NO;
//--
UIImage *image = [UIImage imageNamed:@"icon_back"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorImage = image;
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = image;
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"barShadow"]];
/**
*shadowimage = [UIImage alloc] init] 时没有横线
*shadowimage = nil 时有一根线
*shadowimage = [UIImage imageNamed:@"barShadow"]] 时有带阴影的线
*/
}
}
#pragma mark -- setter and getter
//--isDefaultBarStyle--
- (void)setIsDefaultBarStyle:(BOOL)isDefaultBarStyle
{
objc_setAssociatedObject(self, @selector(isDefaultBarStyle), @(isDefaultBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isDefaultBarStyle
{
NSNumber *number = objc_getAssociatedObject(self, _cmd);
if (number) {
return number.boolValue;
}
self.isDefaultBarStyle = YES;
return YES;
}
//--wnBarTintColor--
- (void)setWnBarTintColor:(UIColor *)wnBarTintColor{
objc_setAssociatedObject(self, @selector(wnBarTintColor), wnBarTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)wnBarTintColor{
return objc_getAssociatedObject(self, _cmd);
}
@end
推荐阅读:
http://www.saturngod.net/articles/uinavigationbar-background-color/
上一篇: button绑定onclick无效
下一篇: php传输数据的方式有哪些?