導(dǎo)航欄的顯示和隱藏問題

需求:看不到導(dǎo)航欄,圖片置頂

697DED41-E0A7-474D-9764-985CD798266C.png

如果簡單的隱藏導(dǎo)航欄或設(shè)置導(dǎo)航欄背景色為透明的話

- (void)viewWillAppear:(BOOL)animated{  
[super viewWillAppear:animated];    
self.navigationController.navigationBar.hidden = YES;   
}

- (void)viewWillDisappear:(BOOL)animated{    
  [super viewWillDisappear:animated];   
 self.navigationController.navigationBar.hidden = NO;
}

會(huì)出現(xiàn)切換時(shí)候有黑色區(qū)域,如下圖


DCA8741C-609D-44C2-9FF2-0925C360601A.png

解決方法:

1、最簡單直接:注意這里一定要用動(dòng)畫的方式隱藏導(dǎo)航欄,這樣在使用滑動(dòng)返回手勢的時(shí)候效果最好.這樣做有一個(gè)缺點(diǎn)就是在切換tabBar的時(shí)候有一個(gè)導(dǎo)航欄向上消失的動(dòng)畫.

- (void)viewWillAppear:(BOOL)animated{     
     [super viewWillAppear:animated];   
     [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{    
    [super viewWillDisappear:animated];    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

2、設(shè)置self為導(dǎo)航控制器的代理,實(shí)現(xiàn)代理方法,在將要顯示控制器中設(shè)置導(dǎo)航欄隱藏和顯示,使用這種方式不僅完美切合滑動(dòng)返回手勢,同時(shí)也解決了切換tabBar的時(shí)候,導(dǎo)航欄動(dòng)態(tài)隱藏的問題.

@interface HMNavigationController ()<UINavigationControllerDelegate>
@end
@implementation HMNavigationController
- (void)viewDidLoad {
    [super viewDidLoad];  
    // 設(shè)置導(dǎo)航控制器的代理為self
    self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {  
  // 判斷要顯示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
注意:問題來了,如果從一個(gè)隱藏導(dǎo)航欄頁面切換到另一個(gè)隱藏導(dǎo)航欄頁面的話,就會(huì)出現(xiàn)導(dǎo)航條慢慢從左往右出現(xiàn)的畫面
1517D610-B533-4078-B0B6-8D82CE19519B.png
最終解決方法:(導(dǎo)航欄的背景圖用無色透明的圖片,下面是用重新畫的一張圖片,此時(shí)導(dǎo)航欄雖然看不到,實(shí)際上是變成了無色透明的了,其實(shí)還是存在的,而且還可以繼續(xù)設(shè)置UIBarButtonItem)
- (void)setNavigationBar {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
}
+ (UIImage *)imageWithColor:(UIColor *)color;{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
并且從一個(gè)無導(dǎo)航欄也面切換到另一個(gè)無導(dǎo)航欄頁面也是可以的,效果圖如下:
abc.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,802評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,080評論 4 61
  • 最好的年華里,記錄下的每一筆都是積累,和成長。 好報(bào)30天寫作群“一天一主題”,竟就這樣從開始到中點(diǎn),到了終點(diǎn)。每...
    紅頭船語閱讀 391評論 3 2
  • 終其一生,我們都在尋找更真實(shí)更美好的自己。自從和瑜伽不期而遇,我就愛上了更平靜的自己。 冥想,隨著...
    空格袖閱讀 1,285評論 0 0
  • 感恩昨晚陪女兒一起讀論語,孩童時(shí)期的記憶力真的很厲害,聽著女兒朗朗上口的背誦令我汗顏的同時(shí),也讓我也再次學(xué)習(xí)學(xué)習(xí)國...
    A曼均閱讀 281評論 0 0

友情鏈接更多精彩內(nèi)容