您的位置:首页 > 产品设计 > UI/UE

IOS开发,UINavigationController属性和基本用法详解

2015-09-10 17:48 543 查看

1,Demo

//1,改变导航控制器的颜色 两种方法
[[UINavigationBar appearance] setBarTintColor:[UIColor purpleColor]];
navi.navigationBar.barTintColor = [UIColor yellowColor];

//2,一般情况,我们都会使用自己的颜色,一般使用下面的宏定义设置颜色:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

//3,在导航栏中使用背景图片
//如果希望在导航栏中使用一个图片当做背景,那么你需要提供一个稍微高一点的图片(这样可以延伸到导航栏背后)。导航栏的高度从44 points(88 pixels)变为了64 points(128 pixels)。我们依然可以使用setBackgroundImage:方法为导航栏设置自定义图片。如下代码所示:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"daohangbar44"] forBarMetrics:UIBarMetricsDefault];//设置44px的图片,状态栏依然是导航栏原来的颜色
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"daohangbar64"] forBarMetrics:UIBarMetricsDefault];//设置64px的图片,状态栏被图片覆盖

//4,导航栏上默认提供的导航按钮颜色是蓝色的,定制颜色的方法
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

//5,使用导航栏的titleTextAttributes属性来定制导航栏的文字风格
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor redColor]; //设置阴影的颜色
shadow.shadowOffset = CGSizeMake(2, 1);//设置阴影的偏移方向
//通过下面这个方法可以设置导航栏标题的风格和样式
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

//6,将导航栏标题修改为一个图片或者logo
//    view.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title"]];
//7,添加多个按钮
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
//系统提供的可以选择的按钮样式类型
{
//        UIBarButtonSystemItemDone,
//        UIBarButtonSystemItemCancel,
//        UIBarButtonSystemItemEdit,
//        UIBarButtonSystemItemSave,
//        UIBarButtonSystemItemAdd,
//        UIBarButtonSystemItemFlexibleSpace,
//        UIBarButtonSystemItemFixedSpace,
//        UIBarButtonSystemItemCompose,
//        UIBarButtonSystemItemReply,
//        UIBarButtonSystemItemAction,
//        UIBarButtonSystemItemOrganize,
//        UIBarButtonSystemItemBookmarks,
//        UIBarButtonSystemItemSearch,
//        UIBarButtonSystemItemRefresh,
//        UIBarButtonSystemItemStop,
//        UIBarButtonSystemItemCamera,
//        UIBarButtonSystemItemTrash,
//        UIBarButtonSystemItemPlay,
//        UIBarButtonSystemItemPause,
//        UIBarButtonSystemItemRewind,
//        UIBarButtonSystemItemFastForward,
//#if __IPHONE_3_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
//        UIBarButtonSystemItemUndo,
//        UIBarButtonSystemItemRedo,
//#endif
//#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
//        UIBarButtonSystemItemPageCurl,
//#endif

}
NSArray *actionButtonItems = @[shareItem, cameraItem];
view.navigationItem.rightBarButtonItems = actionButtonItems;
//8,修改状态栏的风格,就是修改状态栏,时间电池等的颜色。 有两种方法
//1)写Navigation的分类,覆盖其中它的方法   有导航控制器的时候,这个要写到导航控制器中
//    -(UIStatusBarStyle)preferredStatusBarStyle
//    {
//        return UIStatusBarStyleLightContent;
//    }
//2)修改plist文件,增减一行代码
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

//9,隐藏状态栏
//有时候我们需要隐藏状态栏,那么此时我们在view controller中override方法prefersStatusBarHidden:即可, 不管有没有导航栏,这个必须写到ViewController中 如下代码所示:
//    - (BOOL)prefersStatusBarHidden
//    {
//        return YES;
//    }
//10,自定义返回按钮
UIView *barLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
barLeftView.backgroundColor = [UIColor yellowColor];
//    view.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barLeftView];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(select:)];
view.navigationItem.leftBarButtonItem = barItem;

2,UINavigationController详解

2.1navigationItem

  我们都知道navigationItem是UIViewController的一个属性,这个属性是为UINavigationController服务的。文档中是这么解释的“The navigation item used to represent the view controller in a parent’s navigation bar. (read-only)”,即navigation item在navigation Bar代表一个viewController,具体一点儿来说就是每一个加到navigationController的viewController都会有一个对应的navigationItem,该对象由viewController以懒加载的方式创建,稍后我们可以在对象中堆navigationItem进行配置,可以设置leftBarButtonItem,
rightBarButtonItem, backBarButtonItem, title以及prompt等属性。前三个每一个都是一个UIBarButtonItem对象,最后两个属性是一个NSString类型描述,注意添加该描述以后NavigationBar的高度会增加30,总的高度会变成74(不管当前方向是Portrait还是Landscape,此模式下navgationbar都使用高度44加上prompt30的方式进行显示)。当然如果觉得只是设置文字的title不够爽,你还可以通过titleview属性指定一个定制的titleview,这样你就可以随心所欲了,当然注意指定的titleview的frame大小,不要显示出界。

例如:

// set rightItem
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Root" style:UIBarButtonItemStyleBordered target:self action:@selector(popToRootVC)];
childOne.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];

// when you design a prompt for navigationbar, the hiehgt of navigationbar will becaome 74, ignore the orientation
childOne.navigationItem.prompt = @"Hello, im the prompt";

2.2,navigationBar中的stack

  这个属性可以算是UINavigationController的灵魂之一,它维护了一个和UINavigationController中viewControllers对应的navigationItem的stack,该stack用于负责navigationbar的刷新。“注意:如果navigationbar中navigationItem的stack和对应的NavigationController中viewController的stack是一一对应的关系,如果两个stack不同步就会抛出异常。

下面举个简单抛出异常的例子:

SvNavChildViewController *childOne = [[SvNavChildViewController alloc] initWithTitle:@"First" content:@"1"];
[self.navigationController pushViewController:childOne animated:NO];
[childOne release];

// raise exception when the stack of navigationbar and navigationController was not correspond
[self.navigationController.navigationBar popNavigationItemAnimated:NO];  当pushViewcontroller的之后,强制把navigationBar中的navigationItem pop一个出去,程序立马挂起。当然这纯粹只是为了验证问题,我想一般的码农没有人会这么写的。

2.3,navigationBar的显示遵循一下几个原则

navigationBar中包含了这几个重要组成部分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title。当一个view controller添加到navigationController以后,navigationBar的显示遵循一下几个原则:

  1)、Left side of the navigationBar

  a)如果当前的viewController设置了leftBarButtonItem,则显示当前VC所自带的leftBarButtonItem。

  b)如果当前的viewController没有设置leftBarButtonItem,且当前VC不是rootVC的时候,则显示前一层VC的backBarButtonItem。如果前一层的VC没有显示的指定backBarButtonItem的话,系统将会根据前一层VC的title属性自动生成一个back按钮,并显示出来。

  c)如果当前的viewController没有设置leftBarButtonItem,且当前VC已是rootVC的时候,左边将不显示任何东西。

  此处注意:5.0中新增加了一个属性leftItemsSupplementBackButton,通过指定该属性为YES,可以让leftBarButtonItem和backBarButtonItem同时显示,其中leftBarButtonItem显示在backBarButtonItem的右边。

  2)、title部分

  a)如果当前VC通过 .navigationItem.titleView指定了自定义的titleView,系统将会显示指定的titleView,此处要注意自定义titleView的高度不要超过navigationBar的高度,否则会显示出界。

  b)如果当前VC没有指定titleView,系统则会根据当前VC的title或者当前VC的navigationItem.title的内容创建一个UILabel并显示,其中如果指定了navigationItem.title的话,则优先显示navigationItem.title的内容。

  3)、Right side of the navigationBar

  a)如果当前VC指定了rightBarButtonItem的话,则显示指定的内容。

  b)如果当前VC没有指定rightBarButtonItem的话,则不显示任何东西。

2.4,UINavigationController的viewControllers属性

  通过该属性我们可以实现一次性替换整个navigationController的层次, 这个过程如果通过setViewControllers:animated:来设置,并指定动画为YES的画,动画将会从当前的navigationController所显示的vc跳转到所设置的目标viewController的最顶层的那个VC,而中间其他的VC将会被直接从VC层级中移除和添加进来(没有动画)。

2.5,topViewController Vs visibleViewController

  topViewController代表当前navigation栈中最上层的VC,而visibleViewController代表当前可见的VC,它可能是topViewController,也可能是当前topViewController present出来的VC。因此UINavigationController的这两个属性通常情况下是一样,但也有可能不同。

本节有参考:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: