IOS 關于導航欄透明

第一步、在需要導航欄透明的頁面進行設置

//允許半透明狀態

[self.navigationController.navigationBar setTranslucent:YES];

//畫一張寬為屏幕寬,高為64的透明圖片

UIColor *color = [UIColor clearColor];

CGRect rect = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 64);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//將透明圖片設為導航欄的背景圖片

[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];

//對超出父視圖的子視圖不進行裁剪

self.navigationController.navigationBar.clipsToBounds = YES;

第二步、將導航欄改為不透明

[self.navigationController.navigationBar setTranslucent:NO];

CGRect rect = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 64);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [ColorRGB(41.0, 41.0, 41.0, 1) CGColor]);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];


self.navigationController.navigationBar.clipsToBounds = NO;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、設置UILabel行間距 NSMutableAttributedString* attrString = [[...
    十年一品溫如言1008閱讀 1,731評論 0 3
  • 轉自:http://www.code4app.com/blog-866962-1317.html1、設置UILab...
    MMOTE閱讀 1,687評論 1 1
  • 原文 在這里總結一些iOS開發中的小技巧,能大大方便我們的開發,持續更新。 1.UITableView的Group...
    無灃閱讀 795評論 0 2
  • 噓!我的靈魂 保持靜默 永遠都不必去走近一個人 哪怕是你寄居多年的身體 他們深諳規則 這些古老而無趣的真理 已在他...
    f3494c632b49閱讀 820評論 34 45
  • 她代替弟弟從軍,奮勇殺敵,后,傳來噩耗說她戰死沙場,死后,她的家人受賞,皇帝甚至親自為弟弟賜婚, 然而,就在弟弟的...
    我跟你去二次元閱讀 386評論 5 2