天天看點

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

隐藏導航欄底部的線條

方法1 (單頁面設定)

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
 [self.navigationController.navigationBar setShadowImage:[UIImage new]];

如果不想影響其他頁面的導航透明度,viewWillDisappear将其設定為nil即可:

 [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
           

方法2(全局設定)

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
           

方法3

設定導航欄底部線條顔色的代碼:

UINavigationBar *navigationBar = self.navigationController.navigationBar; 
[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; //此處使底部線條顔色為紅色
[navigationBar setShadowImage:[UIImage imageWithColor:[UIColor redColor]]];
           
@implementation UIImage (ColorImage)
+ (UIImage *)imageWithColor:(UIColor *)color{ 
CGRect rect = CGRectMake(f, f, f, f); 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [color CGColor]); 
CGContextFillRect(context, rect); 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); return image;
}@end
           

修複navigationController側滑關閉失效的問題

隐藏傳回按鈕後面的文字

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
           

本文轉自: http://www.jianshu.com/p/aa547432eae0