iOS导航栏基础效果配置[通俗易懂]

iOS导航栏基础效果配置

大家好,又见面了,我是你们的朋友全栈君。

  • 标题设置
self.navigationItem.title = @"标题";
复制代码

正常情况下,控制器的标题会默认作为导航标题

  • 前景色
 self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
复制代码
  • 背景色
//注意上层有毛玻璃遮挡
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
复制代码
  • 背景图
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Background"] forBarMetrics:UIBarMetricsDefault];
复制代码
  • 状态栏字体颜色和隐藏

在iOS7之前

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 
[[UIApplication sharedApplication] setStatusBarHidden:YES];
复制代码

iOS7之后

- (UIStatusBarStyle)preferredStatusBarStyle {  
    return UIStatusBarStyleLightContent;  
}  
  
- (BOOL)prefersStatusBarHidden {  
    return YES;  
} 
复制代码

若iOS7之后仍要使用第一种方法全局设置,则需要在plist文件中添加View controller-based status bar appearance 字段,值为NO ,意为不使用控制器管理状态栏。

  • 设置返回按钮
//只设置颜色
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
复制代码
//设置成图片
UIImage *leftButtonImage = [[UIImage imageNamed:@"image"]
                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                            
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftButtonImage
 style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
复制代码
//设置成文字
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlain target:self action:@selector(back)];
复制代码
//自定义视图
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
复制代码
  • 修复navigationController侧滑手势失效的问题
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
 // 控制手势在根控制器时不触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return self.childViewControllers.count > 1;
}
复制代码
  • 导航栏透明和底部分隔线
//设置透明的背景图,便于识别底部线条有没有被隐藏
[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
//此处使底部线条透明
[navigationBar setShadowImage:[UIImage new]];
复制代码

另外可以通过颜色转图片来修改导航条底部分隔线颜色

//动态地改变UIColor的alpha属性可以返回,不同alpha的图片;可用于动态改变导航条的透明度
+ (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;
}

复制代码
  • 全局设置导航栏外观
//全局设置导航栏主题,只在AppDelegate中有效,
或者是UINavagaitonController中的RootController 中设置有效
- (void)setNavigationControllerAppearance {
    [UINavigationBar appearance].barStyle  = UIBarStyleBlack;
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}

//也可以设置成图片
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"]  forBarMetrics:UIBarMetricsDefault];
复制代码
  • 在导航栏上添加多个按钮
 //方法二
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
        leftButton.backgroundColor = [UIColor clearColor];
        leftButton.frame = CGRectMake(0, 0, 45, 40);
        [leftButton setImage:[UIImage imageNamed:@"LeftButton_back_Icon"] forState:UIControlStateNormal];
        [leftButton setTitle:@"返回" forState:UIControlStateNormal];
        leftButton.tintColor = [UIColor whiteColor];
        leftButton.autoresizesSubviews = YES;
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        leftButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        [leftButton addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

        self.navigationItem.leftBarButtonItems = @[backItem,closeItem];
复制代码
  • 隐藏导航栏
self.navigationController.navigationBar.hidden = YES;
复制代码
  • 导航栏的动态消失
if (scrollView.contentOffset.y > 64) {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}else{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
复制代码

注意:两种方法都是可以隐藏导航栏的,隐藏之后依然可以使用push和pop方法。但是如果用navigationBar.hidden隐藏导航栏,我们可以继续使用navigationBarHidden提供的滑动pop效果,如果用navigationBarHidden,这个操作将无效;但前者navigationBar.hidden没有系统自动的动画效果。

  • 状态栏的自适应问题
//不让其自动调整
self.automaticallyAdjustsScrollViewInsets = NO;
复制代码
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107423.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python二级考试知识点(史上最全)

    Python二级考试知识点(史上最全)Python二级考试知识点(一)1、Python语言基本语法元素考点1.1程序的基本语法元素:程序的框架、缩进、注释、变量、命名、保留字、数据类型、赋值语句、库引用33个保留字6种数据类型4种引用方法:import库、from库import函数、from库impor*、import库as别名考点1.2基本输入输出函数:input()、eval()、print()考点1.3源程序的书写风格-Python之禅运行importthis即可出现考点1.4Pytho

    2022年5月20日
    116
  • iOS:分割控制器UISplitViewcontroller

    iOS:分割控制器UISplitViewcontroller

    2021年9月10日
    61
  • pytest 执行用例_测试用例执行结果有哪些

    pytest 执行用例_测试用例执行结果有哪些前言平常我们功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间

    2022年7月31日
    3
  • RESTful API 设计指南

    RESTful API 设计指南

    2021年11月6日
    28
  • JRtplib开发笔记(四):JRtplib的VS开发环境搭建以及Demo

    原博主博客地址:https://blog.csdn.net/qq21497936本文章博客地址:https://blog.csdn.net/qq21497936/article/details/84957708《JRtplib开发笔记(一):JRtplib简介、JThread库编译》:https://blog.csdn.net/qq21497936/article/details/8478…

    2022年4月17日
    48
  • notify是object方法吗_wait方法和notify方法

    notify是object方法吗_wait方法和notify方法区别notify:只会唤醒等待该锁的其中一个线程。notifyAll:唤醒等待该锁的所有线程。既然notify会唤醒一个线程,并获取锁,notifyAll会唤醒所有线程并根据算法选取其中一个线程获取锁,那最终结果不都是只有一个线程获取锁吗?那JDK为什么还需要做出来这两个方法呢?这两种同步方法本质上会有什么区别?这还要从对象内部锁的调度说起。对象内部锁其实,每个对象都拥有两个池,分…

    2022年9月7日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号