1. 程式人生 > >iOS之導航漸變---/導航透明/隱藏導航欄以及手勢返回遇到的問題,狀態列,tabbarItem角標

iOS之導航漸變---/導航透明/隱藏導航欄以及手勢返回遇到的問題,狀態列,tabbarItem角標

//  CZNavTableViewController.h

//  導航欄漸變透明效果

#import <UIKit/UIKit.h>

@interface CZNavTableViewController :UITableViewController

@end

================

//  CZNavTableViewController.m

//  導航欄漸變透明效果

//

#import "CZNavTableViewController.h"

#import "UINavigationBar+alpha.h"

@interfaceCZNavTableViewController ()

@end

@implementation

CZNavTableViewController

- (void)viewDidLoad {

    [superviewDidLoad];

//

//    [self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];

//

//    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

//    [self.navigationController.navigationBar setTintColor:[UIColor redColor]];

//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//當前的y

CGFloat OffsetY = scrollView.contentOffset.y;

UIColor *color = [UIColorredColor];

CGFloat alpha = (30 +64 - OffsetY)/

64;

if (OffsetY >30) {

        [self.navigationController.navigationBaralphaNavigationBarView:[colorcolorWithAlphaComponent:alpha]];

    }else

    {

        [self.navigationController.navigationBaralphaNavigationBarView:[colorcolorWithAlphaComponent:1]];

    }

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

return0;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

return0;

}

=================

#import <UIKit/UIKit.h>

@interface UINavigationBar (alpha)

//動態新增的UIview新增到UINavigationBar

@property (nonatomic ,strong)UIView *alphaView;

- (void)alphaNavigationBarView:(UIColor *)color;

@end

===========

#import "UINavigationBar+alpha.h"

#import <objc/runtime.h>

@implementation UINavigationBar (alpha)

staticchar alView;

-(UIView *)alphaView

{

returnobjc_getAssociatedObject(self, &alView);

}

-(void)setAlphaView:(UIView *)alphaView

{

objc_setAssociatedObject(self, &alView, alphaView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

//通過這個方法去改變顏色和透明度

-(void)alphaNavigationBarView:(UIColor *)color

{

if (!self.alphaView) {

//設定一張圖片,如果不設定,顏色不純

        [selfsetBackgroundImage:[UIImagenew]forBarMetrics:UIBarMetricsDefault];

//建立

self.alphaView = [[UIViewalloc]initWithFrame:CGRectMake(0, -20,[UIScreenmainScreen].bounds.size.width , 64)];

//新增到navigationbar

        [selfinsertSubview:self.alphaViewatIndex:0];

    }

    [self.alphaViewsetBackgroundColor:color];

}

@end

===============方法二==========在需要設定導航透明的方法中呼叫下面的方法;

- (void)setNavigationController

{

    [self.navigationController.navigationBarsetBackgroundImage:[selfcreateImageWithColor:[UIColorclearColor]]forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBarsetShadowImage:[selfcreateImageWithColor:[UIColorclearColor]]];

    [self.navigationController.navigationBarsetTintColor:[UIColorwhiteColor]];

    [self.navigationController.navigationBarsetTranslucent:YES];

}

-(UIImage *) createImageWithColor: (UIColor *) color

{

CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context =UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [colorCGColor]);

CGContextFillRect(context, rect);

UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return theImage;

}


=======方法三=====clearo是一張透明的PNG圖片;

[self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"clearo"]forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBarsetShadowImage:[selfcreateImageWithColor:[UIColorclearColor]]];//這個是導航下面的陰影線,如果用的是透明背景,這個可以不用顯示。


*******************隱藏導航欄方法一:

注意這裡一定要用動畫的方式隱藏導航欄,這樣在使用滑動返回手勢的時候效果最好,和上面動圖一致.這樣做有一個缺點就是在切換tabBar的時候有一個導航欄向上消失的動畫.

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

=======上面的方法如果隱藏不掉可以在viewdidArppear中設定[self.navigationController setNavigationBarHidden:YES animated:YES];方法二:呼叫navigation的代理方法self.navigationcontroller.delegate=self;

#pragma mark - UINavigationControllerDelegate

// 將要顯示控制器-----隱藏導航

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

// 判斷要顯示的控制器是否是自己

BOOL isShowHomePage = [viewControllerisKindOfClass:[selfclass]];

    [self.navigationControllersetNavigationBarHidden:isShowHomePageanimated:YES];

}


==========去掉導航欄下部的黑線

去掉導航欄self.navigationController.navigationBar下預設黑線。

方法一:(會影響導航欄的translucent透明屬性

  1. //檢視將要顯示時隱藏
  2. -(void)viewWillAppear:(BOOL)animated  
  3. {  
  4.     [super viewWillAppear:animated];  
  5.     [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];  
  6.     [self.navigationController.navigationBar setShadowImage:[UIImage new]];  
  7. }  
  8. //檢視將要消失時取消隱藏
  9. -(void)viewWillDisappear:(BOOL)animated  
  10. {  
  11.     [super viewWillDisappear:animated];  
  12.     [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];  
  13.     [self.navigationController.navigationBar setShadowImage:nil];  
  14. }  
方法二:
  1. @property (nonatomic, weak) UIImageView *lineView;  
  2. //檢視載入完成獲取到導航欄最下面的黑線
  3. - (void)viewDidLoad {  
  4.     [super viewDidLoad];  
  5.     //獲取導航欄下面黑線
  6.     _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];  
  7. }  
  8. //檢視將要顯示時隱藏
  9. - (void)viewWillAppear:(BOOL)animated  
  10. {  
  11.     [super viewWillAppear:animated];  
  12.     _lineView.hidden = YES;  
  13.     self.navigationController.navigationBar.translucent = YES;  
  14.     self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];  
  15. }  
  16. //檢視將要消失時取消隱藏
  17. - (void)viewWillDisappear:(BOOL)animated  
  18. {  
  19.     [super viewWillDisappear:animated];  
  20.     _lineView.hidden = NO;  
  21.     self.navigationController.navigationBar.translucent = NO;  
  22.     self.navigationController.navigationBar.barTintColor = [UIColor blackColor];  
  23. }  
  24. //找到導航欄最下面黑線檢視
  25. - (UIImageView *)getLineViewInNavigationBar:(UIView *)view  
  26. {  
  27.     if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {  
  28.         return (UIImageView *)view;  
  29.     }  
  30.     for (UIView *subview in view.subviews) {  
  31.         UIImageView *imageView = [self getLineViewInNavigationBar:subview];  
  32.         if (imageView) {  
  33.             return imageView;  
  34.         }  
  35.     }  
  36.     return nil;  
=======手勢返回遇到的問題參考:http://blog.csdn.net/yz_lby/article/details/49082131
http://www.jianshu.com/p/7706a8a33b2d
http://blog.csdn.net/jasonblog/article/details/28282147
http://blog.csdn.net/lvxiangan/article/details/51042806
從iOS7.0後蘋果自帶了側滑返回手勢功能interactivePopGestureRecognizer,但是有時我們自定義返回按鈕或者隱藏了導航欄,側滑返回就會失效;

//當自定義返回按鈕時,手勢返回失效,用下面的設定能開啟手勢滑動

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

// 手勢右滑返回---開啟

    if ([self.navigationControllerrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

    }

}


//

//手勢右滑返回禁用

self.navigationController.interactivePopGestureRecognizer.enabled=NO;

//手勢右滑返回開啟

self.navigationController.interactivePopGestureRecognizer.enabled=YES;

*************如果上面的手勢返回不能禁止的話,可以在viewDidload中直接加入下面這幾句話:

id traget =self.navigationController.interactivePopGestureRecognizer.delegate;

    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc]initWithTarget:tragetaction:nil];

 [self.view addGestureRecognizer:pan];


*********如果返回手勢失效的話:可以重寫手勢返回的方法:setp1:需要獲取系統自帶滑動手勢的target物件
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
setp2:建立全屏滑動手勢~呼叫系統自帶滑動手勢的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
step3:設定手勢代理~攔截手勢觸發
pan.delegate = self;
step4:別忘了~給導航控制器的view新增全屏滑動手勢

相關推薦

iOS導航漸變---導航透明/隱藏導航以及手勢返回遇到的問題,狀態,tabbarItem

//  CZNavTableViewController.h//  導航欄漸變透明效果#import <UIKit/UIKit.h>@interface CZNavTableViewController :UITableViewController@end====

Java JFrame隱藏標題以及最大化最小化關閉和拖動

CSDN上第一篇部落格,請大家多多支援! // 轉載請註明出處! 直入主題吧,目前主流的軟體,圖形化介面一般都沒有標題欄,優點是簡單大方。然而,介面的最大化最小化和關閉按鈕也隨之隱藏,窗體也無法拖動。因此,我總結了一些解決這些問題的思路,僅供

iOS旅--隱藏(去除)導航底部橫線

iOS開發大部分情況下會使用到導航欄,由於我司的app導航欄需要與下面緊挨著的視窗顏色一致,導航欄底部的橫線就會影響這個美觀,LZ使用了以下方法。覺得不錯,分享來給小夥伴們。 1)宣告UIImageView變數,儲存底部橫線 @interface M

貓貓學iOS 微博項目實戰(2)微博主框架-自己定義導航控制器NavigationController

點擊狀態 reat obj mar all func 返回 mutable point 貓貓分享,必須精品 原創文章。歡迎轉載。轉載請註明:翟乃玉的博客 地址:viewmode=contents">http://blog.csdn.net/u0133

iOS開發-隱藏導航

ack logs 開發 first control bject uiview bar .cn 去除navigationBar上那條線: ///隱藏navigationBar導航欄線(直接寫在UINavigationController-viewDidLoad方法裏面

iOS側滑返回隱藏導航的VC,導航會閃現一次

ont 隱藏 過程 style nav div nba 周期 導航 VCA:是一個隱藏導航欄的頁面;VCA在ViewWillAppear生命周期函數中設置導航欄隱藏: //隱藏導航欄 [self.navigationController setNavigationBarH

iOS 隱藏導航後,UITableView向下偏移狀態高度 筆記

解決辦法 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else {

iOS 隱藏導航後,UITableView向下偏移狀態高度 筆記

解決辦法 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewC

Android開發應用狀態導航透明

  直接上程式碼 //狀態列、導航欄都透明 private void hideStatusBarNavigationBar() { if (Build.VERSION.SDK_INT = Build.VERSION_CODES.LOLLIPOP)

iOS自定義導航按鈕UIBarButtonItem的樣式

在一個APP中導航的重要性和方便性自然不需要多說了,由於系統的導航用起來實在不怎麼友好,一直想抽個時間把導航學習下 由於投入到工作的時間多些,懶懶散散的一直都是用的時候才去找度娘,一直沒來個總結,前段時間在群裡和別人討論的 時候我說自定義導航不就是隱藏系統的,自己新增一個U

iOS隱藏導航返回上一介面的back

方法一: UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@”” style:UIBarButtonItemStylePl

iOS從當前隱藏導航介面push到下一個顯示導航介面出現閃一下的問題

如果有朋友遇到從當前隱藏導航介面push到下一個顯示導航介面出現閃一下的問題, 下面是我寫的一種方案,也就是在loadView這個生命週期函式中呼叫一個顯示導航條,就 可以解決這個問題: - (voi

iOS導航隱藏的情況下設定狀態顏色

背景介紹: 有的專案要求在tabbat管理的控制器內,有個介面是H5介面,一般H5介面有自己的導航欄,所以在切換到當前H5介面的時候,需要隱藏native(iOS端)的導航欄。 導航欄隱藏方式: - (void)viewDidLoad { [super viewDi

Android開發 動態顯示和隱藏狀態導航

// //佈局填充狀態列,設定透明 // if (Build.VERSION.SDK_INT >= 21) { // View decorView = getWindow().getDecorView(); // int optio

iOS導航的設定與UITabBarController的屬性設定

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)laun

iOS隱藏導航底部的線條& UINavigationBar小技巧

隱藏導航欄底部的線條 方法1 (單頁面設定) [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefa

iOS開發UI篇—使用storyboard創建導航控制器以及控制器的生命周期

拖拉 代理 生命周期 數據 app inter 它的 nsarray itl iOS開發UI篇—使用storyboard創建導航控制器以及控制器的生命周期 一、基本過程 新建一個項目,系統默認的主控制器繼承自UIViewController,把主控制器兩個文件刪掉。 在st

uwp - 上滑隱藏導航下滑顯示

原文: uwp - 上滑隱藏導航欄下滑顯示   好久沒寫部落格了,因為忙著工作。昨天週末填坑需要做一個上滑列表資料時隱藏導航欄下滑時顯示的效果,下面分享一下我的做法,希望能給你帶來幫助。   思路是通過判斷滾動條是往下還是往上滑然後做出相應的顯示隱藏導航欄處理即可;

Android App 隱藏標題+狀態+導航

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

android介面UI美化:沉浸模式、全透明或半透明狀態導航的實現

android api19開始我們就能對頂部狀態列和底部導航欄進行半透明處理了,而api21開始則可以實現全透明狀態列與導航欄以及開啟沉浸模式,至於什麼是沉浸模式,大家百度一下應該就都知道了,有一點需要強調的是全透明不是沉浸模式,前者只是將狀態列、導航欄的背景設定為完全透明,而後者則是完全將狀態列與