前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【IOS开发基础系列】Navigation页面导航专题

【IOS开发基础系列】Navigation页面导航专题

作者头像
江中散人_Jun
发布2023-10-16 11:39:41
3170
发布2023-10-16 11:39:41
举报
IOS开发之UINavigationController详解
http://www.open-open.com/lib/view/open1390611231914.html

1 原理

1.1 navigationController

        对于父级VC与子级VC分别有navigationController的情况,即不是使用push方式加载子VC,而是通过AddChildViewController的方式添加的场景,则父级导航条会覆盖在子级导航条上面,所以需要在载入时把父级导航条做隐藏处理:

1.2 navigationItem

        在含有导航条的ViewController中,VC的navigationItem与VC.navigationController中的 navigationItem并不是同一个对象,如下图所示。

1.3 示例

[self.flagshipStoreTabBarItem setTitle: @"发现"];

//    [self.flagshipStoreTabBarItem setImage: [UIImage imageNamed: @"FhipedIcon"]];

[self.flagshipStoreTabBarItem setFinishedSelectedImage: [UIImage imageNamed: @"FhipedIcon"] withFinishedUnselectedImage: [UIImage imageNamed: @"FhipIcon"]];

UINavigationController view层级

2 开发技巧

2.1 iOS UINavigationController与UITabBarController的组合使用

http://sinye.iteye.com/blog/2093281

(Good)【IOS开发】UITabBarController和UINavigationController结合使用。

http://blog.sina.com.cn/s/blog_721cd3390101vr2d.html

2.2 初始化

2.2.1 初始化titleView

        titleView要在具体ViewController中实现,而不是在NavigationViewController中实现。后者显示不出来。

2.3 回退按钮

2.3.1 回退按钮自定义

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @"BackNavIcon"] style: UIBarButtonItemStylePlain target: self action: @selector(backTo)]; 

[backItem setTitle:@"Test"];

[self.navigationItem setLeftBarButtonItem: backItem];

错误做法:

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] init];//BackNavIcon

[backItem setTitle: @"Test"];

[backItem setImage: [UIImage imageNamed: @"ForeIcon"]];

[self.navigationItem setBackBarButtonItem: backItem];

【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转

http://www.tuicool.com/articles/BZNVza

2.3.2 回退按钮用图标+文字

Creating a left-arrow button (like UINavigationBar's “back”style) on a UIToolbar

http://stackoverflow.com/questions/227078/creating-a-left-arrow-button-like-uinavigationbars-back-style-on-a-uitoolba

2.3.3 手动添加并呈现带导航条的子VC

HJAddVC * addVC = [[HJAddVC alloc] init];

UINavigationController * addNavController = [[UINavigationController alloc] initWithRootViewController: addVC];

[addNavController addChildViewController: addVC];

[rootVC.view addSubview: addNavController.view];

[rootVC addChildViewController: addNavController];

[addVC.view setFrame: rootVC.view.frame];

[addVC.view setBackgroundColor: [UIColor whiteColor]];

2.3.4 手动移除带导航条的子VC

视图View与控制器都需要移除,缺一不可

- (void) closeView: (id)sender

{

    [self.navigationController.view removeFromSuperview];

    [self.navigationController removeFromParentViewController];

}

2.3.5 回退按钮隐藏

[self.navigationController.navigationItem setHidesBackButton: YES];

2.4 导航条

2.4.1 透明导航条

//透明导航栏

[self.navigationController.navigationBar setBackgroundImage: pressedColorImg forBarMetrics: UIBarMetricsDefault];

2.5 常见开发问题

2.5.1 tableView被导航栏遮挡

IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决

http://www.cnblogs.com/PleaseInputEnglish/p/3498032.html

2.5.2 导航条设置为半透明

        将NavigationBar设置透明(仅将指定视图控制器进行透明处理),步骤如下:

    1.在视图控制器的头文件中实现UINavigationControllerDelegate,例如:

@interface PicturePreviewViewController: UIViewController

    2.在实现类中加入这个代理的方法及具体操作如下:

- (void) navigationController: (UINavigationController *)navigationController willShowViewController: (UIViewController*) viewController animated: (BOOL)animated{

    //如果进入的是当前视图控制器

    if (viewController == self) {

        //背景设置为黑色

        self.navigationController.navigationBar.tintColor= [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];

        //透明度设置为0.3

        self.navigationController.navigationBar.alpha= 0.300;

       //设置为半透明

       self.navigationController.navigationBar.translucent = YES; 

    } else {

       //进入其他视图控制器

       self.navigationController.navigationBar.alpha = 1;

       //背景颜色设置为系统默认颜色   

       self.navigationController.navigationBar.tintColor = nil;

       self.navigationController.navigationBar.translucent = NO; 

   }

}

navigationBar translucent

http://blog.csdn.net/yongyinmg/article/details/39957741

2.5.3 去掉回退按钮文字

        最近iOS项目中要求导航栏的返回按钮只保留那个箭头,去掉后边的文字,在网上查了一些资料,最简单且没有副作用的方法就是:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment: UIOffsetMake(0, -60)

 forBarMetrics: UIBarMetricsDefault];

参考自这里:http://stackoverflow.com/questions/19078995/removing-the-title-text-of-an-ios-7-uibarbuttonitem

2.5.4 隐藏返回按钮

[self.navigationItem setHidesBackButton: YES];

2.5.5 设置导航栏标题的字体颜色和大小

    方法一:(自定义视图的方法,一般人也会采用这样的方式)

        就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了。

//自定义标题视图

UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 44)];

titleLabel.backgroundColor = [UIColor grayColor];

titleLabel.font = [UIFont boldSystemFontOfSize: 20];

titleLabel.textColor = [UIColor greenColor];

titleLabel.textAlignment = NSTextAlignmentCenter;

titleLabel.text = @"新闻";

self.navigationItem.titleView = titleLabel;

    方法二:(在默认显示的标题中直接修改文件的大小和颜色也是可以的)

[self.navigationController.navigationBar setTitleTextAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize: 19], NSForegroundColorAttributeName: [UIColor redColor]}];

        方式二相对于方式一而言更加简单方便

2.5.6 导航条上白色按钮变成了蓝色

//声明这张图片用原图(别渲染),默认有亮蓝色渲染

UIImage *img = [UIImage imageNamed: @"ShareIcon"];

img = [img imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

_shareItem = [[UIBarButtonItem alloc] initWithImage: img style:UIBarButtonItemStylePlain target: self action: @selector(shareTo)];

2.5.7 TabBar与导航条混用时,TabBarItem的设置是在NavigationController中,而不是内容Controller中,切记!!!否则会导致页面切换时选中状态不准确

        TabBar与导航条混用时,TabBarItem的设置是在NavigationController中,而不是内容Controller中,切记!!!否则会导致页面切换时选中状态不准确。

2.5.8 Tabbar的显示与隐藏

Tabbar的隐藏函数,其实只在Nav Push的之前调用时起作用

//隐藏Tabbar

[viewController setHidesBottomBarWhenPushed: YES];

[super pushViewController: viewController animated: animated];

而Tabbar的显示,则只有在Pop函数调用前执行才真正起作用

//显示Tabbar

if ([viewController isKindOfClass: [RootVC class]]) {

        [viewController setHidesBottomBarWhenPushed: NO];

}

return [super popToViewController: viewController animated: animated];

3 参考链接

自定义iOS 7 导航栏背景,标题和返回按钮文字颜色

http://blog.csdn.net/mad1989/article/details/41516743

IOS自定义导航栏题目和返回按钮标题

http://blog.csdn.net/hengshujiyi/article/details/29864339

UINavigationBar自定义返回按钮的设置

http://blog.sina.com.cn/s/blog_bf9843bf0101g01b.html

uibarbuttonitem image蓝色

http://www.cocoachina.com/bbs/read.php?tid-180226-page-1.html

(good)iOS 7 UITabBar自定义选中图片显示为默认蓝色的Bug

http://thierry-xing.iteye.com/blog/2171602

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 原理
    • 1.1 navigationController
      • 1.2 navigationItem
        • 1.3 示例
        • 2 开发技巧
          • 2.1 iOS UINavigationController与UITabBarController的组合使用
            • 2.2 初始化
              • 2.2.1 初始化titleView
            • 2.3 回退按钮
              • 2.3.1 回退按钮自定义
              • 2.3.2 回退按钮用图标+文字
              • 2.3.3 手动添加并呈现带导航条的子VC
              • 2.3.4 手动移除带导航条的子VC
              • 2.3.5 回退按钮隐藏
            • 2.4 导航条
              • 2.4.1 透明导航条
            • 2.5 常见开发问题
              • 2.5.1 tableView被导航栏遮挡
              • 2.5.2 导航条设置为半透明
              • 2.5.3 去掉回退按钮文字
              • 2.5.4 隐藏返回按钮
              • 2.5.5 设置导航栏标题的字体颜色和大小
              • 2.5.6 导航条上白色按钮变成了蓝色
              • 2.5.7 TabBar与导航条混用时,TabBarItem的设置是在NavigationController中,而不是内容Controller中,切记!!!否则会导致页面切换时选中状态不准确
              • 2.5.8 Tabbar的显示与隐藏
          • 3 参考链接
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档