一、UINavigationBar 的介绍

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

iOS学习(UI)知识点整理

一、UINavigationBar 的介绍 1)概念:UINavigationBar 是用于定义导航栏按钮的一个类对象 2)在使用UINavigationBar之前必须先初始化导航栏 实例代码:1//初始化导航栏2FirstViewController *firstVC =[[FirstViewController alloc] init];3UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:firstVC];4self.window.rootViewController =nav;56//appearance一定要在初始化之前使用7//修改默认的UINavigationBar的导航条背景颜色,8[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];910//修改默认的导航栏文字即图标颜色11[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];1213//设置导航栏背景图片14[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"icon"] forBarMetrics:UIBarMetricsDefault];

3)setNavigationBarHidden 设置隐藏导航栏 例如: 1[self.navigationController setNavigationBarHidden:YES];

4)title 设置导航栏标题 例如: 1self.title =@“First View”;2//注意:在一处做此设置后,后面的视图控制器如未作设置也会使用此标题

5)titleView 用于设置导航栏中间的视图 例如: 1UIView *navBarView =[[UIView alloc] init];2navBarView.frame = CGRectMake(0,0,100,30);3navBarView.backgroundColor =[UIColor clearColor];4navBarView.layer.cornerRadius =8.0f;5navBarView.clipsToBounds =YES;67UIButton *btn1 =[[UIButton alloc] init];8btn1.frame = CGRectMake(0,0,50,30);9btn1.backgroundColor =[UIColor blueColor];10[btn1 setTitle:@"消息"forState:UIControlStateNormal];11[btn1 addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];12btn1.tag =1000;13[navBarView addSubview:btn1];1415UIButton *btn2 =[[UIButton alloc] init];16btn2.frame = CGRectMake(50,0,50,30);17btn2.backgroundColor =[UIColor blueColor];18[btn2 setTitle:@"电话"forState:UIControlStateNormal];19[btn2 addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];20btn2.tag =1001;21[navBarView addSubview:btn2];2223//在导航栏中的中间位置加入我们自定义的view,24//程序会把我们设置的view自动居中25self.navigationItem.titleView = navBarView;

6)UIBarButtonItem 导航栏上的按钮元素 常用的系统自带的Bar有 /*

* UIBarButtonSystemItemDone 按钮样式为文字Done、

* UIBarButtonSystemItemAdd 按钮样式为图片的加号

*UIBarButtonSystemItemCamera 按钮样式是图片的照相机

*UIBarButtonSystemItemFixedSpace 是一个占位符 ,可以设置width

*UIBarButtonSystemItemFlexibleSpace 是一个占位符,固定宽度,导航栏上单独一个按钮

*/

例如:

1//系统自带照相机按钮2UIBarButtonItem *button1 =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera

target:self action:@selector(barButtonTapped:)];34//占位符按钮5UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:self action:@selector(barButtonTapped:)];

7)rightBarButtonItem 设置导航栏右侧单个按钮 例如:1self.navigationItem.rightBarButtonItem = button1;

8)rightBarButtonItems 设置导航栏右侧多个按钮 例如:1self.navigationItem.rightBarButtonItems = @[button2, button1];

9)edgesForExtendedLayout 设置view的坐标都是从导航栏左下点开始计算 防止导航栏遮挡内容区域

例如:1self.edgesForExtendedLayout = UIRectEdgeNone;

10)initWithTitle 使用文字作为导航栏按钮 例如:1UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlain

target:self action:@selector(back)];

11)initWithImage 使用图片作为导航栏按钮 例如:1UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"st_logout"]

style:UIBarButtonItemStylePlain target:self action:@selector(back)];2self.navigationItem.leftBarButtonItem = barButtonItem;

12)setToolbarHidden 设置导航栏显示与隐藏 例如: 1[self.navigationController setToolbarHidden:NO];

13)动画效果实现 代码:1- (void)viewDidLoad {2[super viewDidLoad];3self.view.backgroundColor=[UIColor whiteColor];45firstView=[[UIView alloc]init];6firstView.frame=CGRectMake(0,20,50,50);7firstView.backgroundColor=[UIColor blackColor];8[self.view addSubview:firstView];9for(inti=0; i<4; i++) {10UIButton *btn=[[UIButton alloc]init];11btn.frame=CGRectMake(20,50, CGRectGetWidth(self.view.frame),30);12}13[self moveFirstViewToRight];14}1516-(void)moveFirstViewToRight{17//UIview 动画18[UIView animateWithDuration:3.f animations:^{19firstView.frame=CGRectMake(self.view.frame.size.width-firstView.frame.size.width,20,50,50);20}];21}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航