1: 修改导航控制器背景图片的方式(IOS5以上)
1
|
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; |
注: 通过appearance可以设置全局的控件初始化外观.不过在初始化成功以后,有单独样式需求亦可用同样的方法修改.
UINavigationBar的标准高度是44,在iOS7之前可以通过44+X的方式实现背景+阴影的效果.从iOS7以后就不行了.
iOS7对UINavigationBar的标准进行重新的定义,其高度可以延伸到状态栏.所以44+20的高度等于64.
而刚刚说的44+X方式不再适用于iOS7,iOS7的新规范是64+1.背景图和阴影将单独来设定,代码如下:
1
2
3
4
|
//iOS7 新背景图片设置方法 高度 必需是 64 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@ "toolbar_background_iOS7Test" ] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault]; //iOS7 阴影需单独设定 UIColor clearColor 是去掉字段 1像素阴影 [self.navigationController.navigationBar setShadowImage:[UIImage Help_imageWithColor:[UIColor clearColor]]]; |
2:为UINavigationBar设置半透明的背景效果:
1
|
[self.navigationController.navigationBar setTranslucent:YES]; |
注:在iOS7中 默认生成 UINavigationBar的translucent属性为YES. 自动添加遮罩模糊效果.
3:修改UINavigationBar的背景颜色(iOS7以上)
1
|
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; |
创建一个navigationController 并给他个视图控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] initWithNibName:@”ViewController” bundle:nil]];
//设定标题
self.title = @”首页”;
//修改为自定义图片 源码例子可以去我那资源里下载
采用继承自UINavigationController 然后重写 – (void)drawRect:(CGRect)rect
隐藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
navigation的推转动画是可以更改的
CATransition *transition = [CATransitionanimation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionDefault];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.navigationController.view.layeraddAnimation:transition forKey:nil];
[self.navigationControllerpopViewControllerAnimated:NO];
navigaiton的出栈有这么几种方式:
[self.navigationController popViewControllerAnimated:YES]; //回到上一个视图控制器
[self.navigationController popToViewController:viewController animated:YES]; //回到某一个视图控制器
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -层次)] animated:YES];
[self.navigationController popToRootViewControllerAnimated:YES]; //回到根视图控制器
//设置Navigation Bar颜色
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
//自定义标题
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor]; //设置Label背景透明
titleLabel.font = [UIFont boldSystemFontOfSize:20]; //设置文本字体与大小
titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1]; //设置文本颜色
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = @"自定义标题"; //设置标题
self.navigationItem.titleView = self.titleLabel;
UIView * tview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
UIImageView * titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”mainTitle.png”]];
[titleView setFrame:CGRectMake(0, 0, 150, 22)];
[tview addSubview:titleView];
self.navigationItem.titleView=tview;
navigationbar 背景 高度问题
/**
* 系统默认 navigationBar 半透明,子视图的原点默认从 屏幕的(0,0)点为参考点,相当于整个视图中不存在navigationbar 这个视图。
navi.navigationBar.translucent = NO; // 设置为不透明,
不透明之后,子视图的参考点,会以 navigationbar 左下方为参考点,即相对于屏幕的(0,64)计算,
自定义 navigationbar 相当于自定义一个ui view ,它实质是一个 ui view。
[navigationBar addSubview:view]便可;
UIBarButtonItem 不是一个 view,它继承自:NSObject ,是一个对象;凡事可见的,才是view
*/
navi.navigationBar.translucent = NO;
[navi.navigationBar setFrame:CGRectMake(0, 20, 320, 40)];
[navi.navigationBar setBarTintColor:CVMainColor];
UIImageView *naviView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CVScreenSize.width, CVNaviBarHeight – 20)];
naviView.backgroundColor = CVMainColor;
//[navi.navigationBar addSubview:naviView];
//[navi.navigationBar setBackgroundImage:[UIImage imageNamed:@”navibar_bg”] forBarMetrics:UIBarMetricsDefault];
转载请注明:夜阑小雨 » iOS的UINavigation的使用