您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 自定義狀態欄與導航欄詳解

編輯:iOS 開發綜合

iOS 自定義狀態欄與導航欄

開發 iOS App 時,經常會依需求調整狀態欄與導航欄,這裡整理了幾種常見做法,供大家參考。

導航欄透明

 -(void)viewWillAppear:(BOOL)animated { //viewWillAppear中設置透明 [super viewWillAppear:animated]; [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; //用空圖片填充機位透明 [self.navigationBar setShadowImage:[UIImage new]];//naviBar底部的seperatorLine } -(void)viewDidDisappear:(BOOL)animated { //viewWillAppear中設置恢復 [super viewDidDisappear:animated]; [self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; [self.navigationBar setShadowImage:shadowImage]; } 

導航欄漸變

 barImageView = self.navigationController.navigationBar.subviews.firstObject; -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat minAlphaOffset = - 64; CGFloat maxAlphaOffset = 200; CGFloat offset = scrollView.contentOffset.y; CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset); _barImageView.alpha = alpha; } 

狀態欄字體顏色改變

 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;//黑色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;//白色

導航欄隱藏

若導航欄自定義程度高,需要完全自行重寫,可以隱藏原本導航欄並定義一個新的 view:

 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBarHidden = YES; } -(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; self.navigationController.navigationBarHidden = NO; } -(void)ys_initNavigationBar { self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; // 保留右滑pop的手勢 _naviBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)]; _naviBar.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_naviBar]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _naviBar.height-0.5, self.view.width, 0.5)]; line.backgroundColor = [UIColor colorForHex:@"f0f0f0"]; [_naviBar addSubview:line]; // 返回 backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 20, 44, 44); [backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backButton]; // 標題 naviLable = [[UIButton alloc] initWithFrame:CGRectMake(44, 20, self.view.width-44*2, 44)]; naviLable.backgroundColor = [UIColor clearColor]; naviLable.font = [UIFont systemFontOfSize:16]; naviLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview: naviLable]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //navigationBar change CGFloat minAlphaOffset = 0; CGFloat maxAlphaOffset = 40; CGFloat offset = scrollView.contentOffset.y; CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset); _naviBar.alpha = alpha; naviLabel.alpha = alpha; } 
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved