iOS 自定义导航栏背景,左侧按钮,右侧按钮,及标题
来源:互联网 发布:yandex优化 编辑:程序博客网 时间:2024/04/30 12:32
一:导航栏的背景颜色如何设置
控制器是UINavigationController
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航栏背景图"] forBarMetrics:UIBarMetricsDefault];
可以利用下面方法的得到各种颜色的背景图片
+ (UIImage *)buttonImageFromColor:(UIColor *)color WithSize:(CGSize)size { CGRect rect = CGRectMake(0, 0, size.width, size.height); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img;}
二:整体设置导航栏字体颜色
控制器是UINavigationController
[self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
三:设置返回按钮
控制器是UINavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"FSNavfanhui"] forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, 44, 44); // 让按钮内部的所有内容左对齐 button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; // 修改导航栏左边的item viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; // 隐藏tabbar viewController.hidesBottomBarWhenPushed = YES; } // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem [super pushViewController:viewController animated:animated];}- (void)back{ [self popViewControllerAnimated:YES];}
四:UIViewController修改导航栏左侧按钮
控制器是UIViewController
#pragma mark - 设置返回按钮- (void)setNavigationLeftBarTitleText:(NSString *)title{ FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44) Title:title TitleColor:HexRGB(0xffffff) BackgroundColor:[UIColor clearColor] Font:SYSTEM_FONT(14) State:UIControlStateNormal]; // 让按钮内部的所有内容左对齐 button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; // 修改导航栏左边的item self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];}#pragma mark - 返回点击事件- (void)back{ [self.navigationController popViewControllerAnimated:YES];}
四、五:UIViewController修改导航栏右侧按钮
.h中右侧按钮的block定义
/** * 右侧按钮点击事件 */@property(nonatomic, copy) void (^fs_rightBtnClickBlock)(FSButton * fs_rightBtn);
1:根据文字定义导航栏右侧按钮
/** * 根据文字设定右侧按钮 */- (void)setNavigationBarRightItemWithTitle:(NSString *)title actionBlock:(void (^)(FSButton *button))actionBlock{ FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44) Title:title TitleColor:HexRGB(0xffffff) BackgroundColor:[UIColor clearColor] Font:SYSTEM_FONT(14) State:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.fs_rightBtnClickBlock = actionBlock;}#pragma mark - 右侧按钮点击事件- (void)fs_rightBtnClick:(FSButton *)sender{ self.fs_rightBtnClickBlock(sender);}
2:根据图片定义导航栏右侧按钮
/** * 根据图片设定右侧按钮方法 */- (void)setNavigationBarRightItemWithImage:(NSString *)imageName actionBlock:(void (^)(FSButton *))actionBlock{ FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44)]; [button setImage:IMAGE_NAMED(imageName) forState:UIControlStateNormal]; [button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.fs_rightBtnClickBlock = actionBlock;}#pragma mark - 右侧按钮点击事件- (void)fs_rightBtnClick:(FSButton *)sender{ self.fs_rightBtnClickBlock(sender);}
3:根据文字设置导航栏右侧的可点击和不可点击文字
/** * 设定右侧按钮方法 */- (void)setNavigationBarRightItemWithTitleForNormal:(NSString *)titleForNormal titleForDisable:(NSString *)titleForDisable colorForNormal:(UIColor *)colorForNormal colorForDisable:(UIColor *)colorForDisable actionBlock:(void (^)(FSButton *))actionBlock{ FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44)]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [button.titleLabel setFont:SYSTEM_FONT(14)]; [button setTitle:titleForNormal forState:UIControlStateNormal]; [button setTitleColor:colorForNormal forState:UIControlStateNormal]; [button setTitle:titleForDisable forState:UIControlStateDisabled]; [button setTitleColor:colorForDisable forState:UIControlStateDisabled]; [button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];// [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:colorForNormal, NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];// [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:colorForDisable, NSForegroundColorAttributeName,nil] forState:UIControlStateDisabled]; self.fs_rightBtnClickBlock = actionBlock;}#pragma mark - 右侧按钮点击事件- (void)fs_rightBtnClick:(FSButton *)sender{ self.fs_rightBtnClickBlock(sender);}
六:导航两侧按钮距离左右侧边距的修改
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame= CGRectMake(0, 0, 44, 44); [btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; /**width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和 边界间距为5pix,所以width设为-5时,间距正好调整为0;width为正数 时,正好相反,相当于往左移动width数值个像素*/negativeSpacer.width = -5; self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
1 0
- iOS 自定义导航栏背景,左侧按钮,右侧按钮,及标题
- iOS 自定义导航栏标题和按钮
- Swift- 自定义导航栏背景、标题和返回按钮属性
- 自定义iOS导航栏背景,标题和返回按钮文字颜色
- iOS 7+自定义导航栏背景,标题和返回按钮文字颜色
- ios之自定义导航栏背景,标题和返回按钮文字颜色
- ios 如何让导航栏上的左侧按钮靠左贴边显示,右侧按钮靠右贴边显示
- IOS 自定义导航栏标题和返回按钮标题
- iOS自定义导航栏标题和返回按钮标题
- IOS 自定义导航栏标题和返回按钮标题
- 自定义导航栏按钮 标题
- IOS自定义导航按钮和标题
- IOS 自定义导航栏题目和返回按钮标题
- IOS 自定义导航栏题目和返回按钮标题
- iOS隐藏系统导航栏左侧返回按钮上的标题
- iOS 11,关于隐藏导航栏左侧返回按钮的标题title
- iOS 11,关于隐藏导航栏左侧返回按钮的标题title
- UINavigationBar自定义导航栏背景和按钮
- mysql binlog
- 使用Java建立聊天客户端
- Android 7.0 新特性
- 16-hive 文件压缩
- docker源码阅读笔记-github配置及代码提交操作
- iOS 自定义导航栏背景,左侧按钮,右侧按钮,及标题
- new delete
- 岁月悠长,秋深情暖
- HDU_2544 标准题解(没有之一)Floyd基础教学
- ftp常用命令介绍
- PAT(乙) 1006. 换个格式输出整数 (15)
- ios开发-AppDelegate
- 我们终会忘记那个ta
- Android Gradle 多版本多APK打包,修改生成APK名称