UINavigationBar的简单实用
来源:互联网 发布:阿里云服务器配置推荐 编辑:程序博客网 时间:2024/05/18 06:23
==================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/
http://blog.sina.com.cn/s/blog_6291e42d0101f6b0.html
阅读全文
0 0
- UINavigationBar的简单实用
- UINavigationBar的简单使用
- IOS:学习添加一个简单的UINavigationBar
- iOS UINavigationBar 完全透明的方法 超级实用
- 给 UINavigationBar 设置背景图片的最简单方法
- 给 UINavigationBar 设置背景图片的最简单方法
- 给 UINavigationBar 设置背景图片的最简单方法
- 为UINavigationBar添加背景图片的几种简单思路
- UINavigationBar添加背景图片的几种简单思路
- 简单实用的bugfree
- 简单实用的 .vimrc
- selenium的简单实用
- 简单实用的makefile
- ArrayList的简单实用
- 简单实用的分享
- wxTimer的简单实用
- GPUImage的简单实用
- UISearchBar的简单实用
- linux下更改mysql密码
- jsp 输出杨辉三角
- 【总结】PHP常见面试题汇总(三)
- TensorBoard使用
- js上传实现进度圆圈以及图片预览
- UINavigationBar的简单实用
- 常见问题3
- ajax请求之post入参为空存在的问题
- Shell echo命令
- YARN 笔记(一)
- 【总结】PHP常见面试题汇总(四)
- Categorical Reparameterization with Gumbel-Softmax
- GreenDao源码解析以及GreenDao封装
- 5.RPC框架的简单实现(SPI机制-ProtocolFactory)