国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

iOS階段學(xué)習(xí)第31天筆記(UINavigationBar介紹)

2019-11-14 18:35:34
字體:
供稿:網(wǎng)友

iOS學(xué)習(xí)(UI)知識點整理

 

一、UINavigationBar 的介紹 

1)概念:UINavigationBar 是用于定義導(dǎo)航欄按鈕的一個類對象 

2)在使用UINavigationBar之前必須先初始化導(dǎo)航欄 實例代碼:

 1 //初始化導(dǎo)航欄 2 FirstViewController *firstVC = [[FirstViewController alloc] init]; 3 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstVC]; 4 self.window.rootViewController = nav; 5  6 //appearance一定要在初始化之前使用 7 //修改默認的UINavigationBar的導(dǎo)航條背景顏色, 8 [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]]; 9 10 //修改默認的導(dǎo)航欄文字即圖標顏色11 [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];12     13 //設(shè)置導(dǎo)航欄背景圖片14 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"icon"]   forBarMetrics:UIBarMetricsDefault];

 3)setNavigationBarHidden 設(shè)置隱藏導(dǎo)航欄  例如: 

1 [self.navigationController setNavigationBarHidden:YES];

 4)title 設(shè)置導(dǎo)航欄標題  例如: 

1 self.title = @“First View”;2 //注意:在一處做此設(shè)置后,后面的視圖控制器如未作設(shè)置也會使用此標題

 5)titleView 用于設(shè)置導(dǎo)航欄中間的視圖 例如: 

 1    UIView *navBarView = [[UIView alloc] init]; 2     navBarView.frame = CGRectMake(0, 0, 100, 30); 3     navBarView.backgroundColor = [UIColor clearColor]; 4     navBarView.layer.cornerRadius = 8.0f; 5     navBarView.clipsToBounds = YES; 6      7     UIButton *btn1 = [[UIButton alloc] init]; 8     btn1.frame = CGRectMake(0, 0, 50, 30); 9     btn1.backgroundColor = [UIColor blueColor];10     [btn1 setTitle:@"消息" forState:UIControlStateNormal];11     [btn1 addTarget:self action:@selector(btn1Tapped:)  forControlEvents:UIControlEventTouchUpInside];12     btn1.tag = 1000;13     [navBarView addSubview:btn1];14     15     UIButton *btn2 = [[UIButton alloc] init];16     btn2.frame = CGRectMake(50, 0, 50, 30);17     btn2.backgroundColor = [UIColor blueColor];18     [btn2 setTitle:@"電話" forState:UIControlStateNormal];19     [btn2 addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];20     btn2.tag = 1001;21     [navBarView addSubview:btn2];22 23     //在導(dǎo)航欄中的中間位置加入我們自定義的view,24     //程序會把我們設(shè)置的view自動居中25     self.navigationItem.titleView = navBarView;

 

6)UIBarButtonItem 導(dǎo)航欄上的按鈕元素  常用的系統(tǒng)自帶的Bar有 

/*
* UIBarButtonSystemItemDone  按鈕樣式為文字Done、
* UIBarButtonSystemItemAdd 按鈕樣式為圖片的加號
*UIBarButtonSystemItemCamera 按鈕樣式是圖片的照相機
     
*UIBarButtonSystemItemFixedSpace 是一個占位符 ,可以設(shè)置width
*UIBarButtonSystemItemFlexibleSpace 是一個占位符,固定寬度,導(dǎo)航欄上單獨一個按鈕
*/

例如:

1 //系統(tǒng)自帶照相機按鈕2 UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:@selector(barButtonTapped:)];
3 4 //占位符按鈕5 UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:@selector(barButtonTapped:)];

 

7)rightBarButtonItem 設(shè)置導(dǎo)航欄右側(cè)單個按鈕 例如:

1 self.navigationItem.rightBarButtonItem = button1;

 

8)rightBarButtonItems 設(shè)置導(dǎo)航欄右側(cè)多個按鈕 例如:

1 self.navigationItem.rightBarButtonItems = @[button2, button1];

 

9)edgesForExtendedLayout 設(shè)置view的坐標都是從導(dǎo)航欄左下點開始計算 防止導(dǎo)航欄遮擋內(nèi)容區(qū)域
例如:

1 self.edgesForExtendedLayout = UIRectEdgeNone;

 

10)initWithTitle 使用文字作為導(dǎo)航欄按鈕 例如:

1 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain
target:self action:@selector(back)];

 

11)initWithImage 使用圖片作為導(dǎo)航欄按鈕 例如:

1 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"st_logout"] 
style:UIBarButtonItemStylePlain target:self action:@selector(back)];
2 self.navigationItem.leftBarButtonItem = barButtonItem;

12)setToolbarHidden 設(shè)置導(dǎo)航欄顯示與隱藏 例如: 

1  [self.navigationController setToolbarHidden:NO];

 

13)動畫效果實現(xiàn) 代碼:

 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     self.view.backgroundColor=[UIColor whiteColor];  4  5     firstView=[[UIView alloc]init]; 6     firstView.frame=CGRectMake(0, 20, 50, 50); 7     firstView.backgroundColor=[UIColor blackColor]; 8     [self.view addSubview:firstView];  9     for (int i=0; i<4; i++) { 10         UIButton *btn=[[UIButton alloc]init]; 11         btn.frame=CGRectMake(20,50, CGRectGetWidth(self.view.frame), 30); 12     } 13     [self moveFirstViewToRight];14 }15 16 -(void)moveFirstViewToRight{ 17     //UIview 動畫18     [UIView animateWithDuration:3.f animations:^{19         firstView.frame=CGRectMake(self.view.frame.size.width-firstView.frame.size.width, 20, 50, 50);20     }];21 }

 

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会宁县| 绥德县| 呼伦贝尔市| 奎屯市| 武川县| 阿合奇县| 平山县| 高淳县| 高唐县| 嘉义市| 肃宁县| 化德县| 镶黄旗| 城固县| 桃江县| 义马市| 舒城县| 东乡族自治县| 阿拉尔市| 双柏县| 盐城市| 墨竹工卡县| 凉山| 颍上县| 嘉义县| 五常市| 永丰县| 方城县| 额尔古纳市| 鹤岗市| 新晃| 锦屏县| 台南市| 平山县| 衡阳县| 荣成市| 体育| 辽源市| 南乐县| 凯里市| 宜宾县|