1. 程式人生 > >iOS開發之給navigationbar和view設定同一張圖片

iOS開發之給navigationbar和view設定同一張圖片

###### 效果一: 螢幕快照 2017-06-09 15.55.31.png

   今天因為這個小東西被吐槽了...但還是決定記錄一下...

其實說是給navigationbar和view設定同一張圖片是不對的,應該是隱藏navigationbar,再去掉statusbar的背景色,新增view

最開始我想成隱藏bar和status,自己仿照status新增一個有網路/時間/電量的view.(因為我事先不知道有方法…),但是覺得這樣腦洞太大了(而且太麻煩),所以便去群裡問了一下…然後你沒看錯…我被群嘲了(huo gai)….

Simona_Test1

後來說了半天有不懂我問題的,有要繼續打我的,最終還是有人丟擲了我想要的答案(此處羞澀(wei suo)一笑)

好吧其實就兩句程式碼(捂臉)

    self.navigationController.navigationBar.hidden = YES; // 隱藏navigationbar
    self.automaticallyAdjustsScrollViewInsets = NO; //隱藏statusbar的白色背景

###### 效果二: Simona_Test2

 self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNavigationBarImageColor:[UIColor redColor]]; // 呼叫


//去除導航欄下方的橫線
- (void)setNavigationBarImageColor:(UIColor *)color {
[self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:color]
                                             forBarPosition:UIBarPositionAny
                                                 barMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];

}

-(UIImage*) createImageWithColor:(UIColor*) color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}