navigationBar
1.标题背景
高度为44 上面的状态栏高度为20
//标题
self.title = @"";
//self.navigationItem.title = @"";
//self.navigationController.title = @"";
注意:如果不是UINavigationController自带的,而是UINavigationBar
UINavigationBar继承于UIView的,它里面没有title属性
//调用方法
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
把UINavigationItem弄到UINavigationBar上,那些title,left,
right就在UINavigationItem设置
//titleTextAttributes 通过它你可以设置title部分的字体
NSDictionary *dict = [NSDictionary dictionaryWithObject:
[UIColor yellowColor] forKey:UITextAttributeTextColor];
self.navigationController.navigationBar.titleTextAttributes
= dict;
//副标题
self.navigationItem.prompt = @"";//高度为40
//更改Bar的颜色
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.tintColor = [UIColor yellowColor];
//背景颜色
navBar.backgroundColor = [UIColor blueColor];
//是否透明
navBar.translucent =YES;
//风格
navBar.barStyle = UIBarStyleBlackOpaque;
// UIBarStyleDefault = 0,
// UIBarStyleBlack = 1,
// UIBarStyleBlackTranslucent = 2
// 自定义背景
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]
{
// UIBarMetricsLandscapePhone
[navBar setBackgroundImage:[UIImage imageNamed:@"图片名称"] forBarMetrics:UIBarMetricsDefault];
}
setBackgroundImage方法的第二个参数:
来显示UINavigationBar
UIBarMetricsLandscapePhone:用横着的时UINavigationBar
的标准尺寸来显示UINavigationBar
2.设置navigationitem
注意:UINavigationItem不是UI,只是一种在UINavigationBar
上显示的一些子控件的描述,比如左按钮、中间标题、右按钮
rightBarButtonItem, backBarButtonItem, title。当一个view controller
原则:
1)、Left side of the navigationBar
a)如果当前的viewController设置了leftBarButtonItem
,则显示当前VC所自带的leftBarButtonItem。
b)如果当前的viewController没有设置leftBarButtonItem,
且当前VC不是rootVC的时候,则显示前一层VC的backBarButtonItem。
根据前一层VC的title属性自动生成一个back按钮,并显示出来。
c)如果当前的viewController没有设置leftBarButtonItem,
且当前VC已是rootVC的时候,左边将不显示任何东西。
同时显示,其中leftBarButtonItem显示在backBarButtonItem的右边。
2)、title部分
a)如果当前VC通过 .navigationItem.titleView指定了自定义的titleView,系统将会显示指定
的titleView,此处要注意自定义titleView
的高度不要超过navigationBar的高度,否则会显示出界。
或者当前VC的navigationItem.title的内容创建一个UILabel并显示,其中
如果指定了navigationItem.title的话,则优先显示navigationItem.title的内容。
3)、Right side of the navigationBar
a)如果当前VC指定了rightBarButtonItem的话,则显示指定的内容。
b)如果当前VC没有指定rightBarButtonItem的话,则不显示任何
东西。
(字符串) 、(图片)UIImage 、 systemitem(系统样式)、自定义样式
UIBarButtonItem* rightbutton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:
UIBarButtonSystemItemUndo target:self
action:@selector(action)];
self.navigationItem.rightBarButtonItem = rightbutton;
//标题
UIBarButtonItem *rightbutton1 = [[UIBarButtonItem alloc] initWithTitle:@"right"
style:UIBarButtonItemStyleBordered
target:self action:@selector(action:)];
//图片
UIBarButtonItem *rightbutton2 = [[UIBarButtonItem alloc] initWithImage:
[UIImage imageNamed:@"图片"]
style:UIBarButtonItemStyleBordered
target:self action:@selector(action:)];
//自定义
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 0, 50, 40);
[button setTitle:@"right" forState:UIControlStateNormal];
[button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightbutton3 = [[UIBarButtonItem alloc]initWithCustomView:button];
//将自定义的segmentegControl添加到titleview的位置中
NSArray *segmentTextContent = @[@"Image",@"Text",@"Video",];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.frame = CGRectMake(0, 0, 400.0f, 30.0f);
[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
3、Toolbar
navigationController自带了一个工具栏,通过设置 self.navigationController.toolbarHidden = NO来显示工具栏,
显示的顺序和设置的NSArray中存放的顺序一致,其中每一个数据都一个
UIBarButtonItem对象,可以使用系统提供的很多常用风格的对象,
也可以根据需求进行自定义。
UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:nil action:nil];
target:nil action:nil];
UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:nil action:nil];
UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:nil action:nil];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObjects:
flexItem, four, flexItem, nil]];
self.navigationController.toolbarHidden = NO;
4、UINavigationControllerDelegate
这个代理真的很简单,就是当一个viewController要显示的
时候通知一下外面,给你一个机会进行设置,包含如下两个函数:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
当你需要对某些将要显示的viewController进行修改的话,可实现该代理。
5、topViewController Vs visibleViewController
topViewController代表当前navigation栈中最上层的VC,而visibleViewController代表当前可见的VC,它可能是topViewController,也可能是当前topViewController present出来的VC。因此UINavigationController的这两个属性通常情况下是一样,但也有可能不同。
8.设置全局navbar
在application: didFinishLaunchingWithOptions:里
// 1.设置导航栏背景
UINavigationBar *bar = [UINavigationBar appearance];
[bar setBackgroundImage:[UIImage imageNamed:@"xx.png"]
forBarMetrics:UIBarMetricsDefault];
// 状态栏
[UIApplication sharedApplication].statusBarStyle =
UIStatusBarStyleLightContent;
// 2.设置导航栏 标题文字属性
NSMutableDictionary *barAttrs = [NSMutableDictionary dictionary];
[barAttrs setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[bar setTitleTextAttributes:barAttrs];
// 3.按钮
UIBarButtonItem *item = [UIBarButtonItem appearance];
[item setBackgroundImage:[UIImage imageNamed:@"xx.png"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
barMetrics:UIBarMetricsDefault];//图片尺寸要设置好,不然出现拉伸
//按钮文字字体属性
NSMutableDictionary *itemAttrs = [NSMutableDictionary
dictionaryWithDictionary:barAttrs];
[itemAttrs setObject:[UIFont boldSystemFontOfSize:30]
forKey:NSFontAttributeName];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateHighlighted];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateDisabled];
// 4.返回按钮
[item setBackButtonBackgroundImage:[UIImage imageNamed:@"Menu@2x.png"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
9.push 动画
UIViewController *ViewController = [[UIViewController alloc]init];
//从底部push view
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[animation setDuration:1.0f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
pushViewController:ViewController animated:NO];
[self.navigationController.view.layer
addAnimation:animation forKey:@"pushView"];
10.之间的跳转
返回根页面vc用 :
[self.navigationController popToRootViewController]
返回指定的某个vc用下面(通过index定位)
[self.navigationController popToViewController
[self.navigationController.viewControllers objectAtIndex:2 animated:YES];
或(通过class定位)
for(UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[你要跳转到的Controller class]])
{
[self.navigationController popToViewController:controller animated:YES];
}
}