Answers:
从这个答案
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
另外,如Josh在评论中所建议,将标尺重新设置为默认值:
[self.navigationController.navigationBar setBackgroundImage:nil
forBarMetrics:UIBarMetricsDefault];
对于Swift3和Swift4
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
对于Swift2.2
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
对于Objective-C
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
自包含解决方案作为Objective-C类别:
UINavigationController + TransparentNavigationController.h
@interface UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
@end
UINavigationController + TransparentNavigationController.m
#import "UINavigationController+TransparentNavigationController.h"
@implementation UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar
{
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:YES];
[self.navigationBar setShadowImage:[UIImage new]];
[self setNavigationBarHidden:NO animated:YES];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
@end
现在,您可以将类别导入您的类别,UIViewController
然后在导航控制器上调用方法-例如:
#import "UINavigationController+TransparentNavigationController.h"
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController presentTransparentNavigationBar];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController hideTransparentNavigationBar];
}
和Swift中的类似解决方案:
import Foundation
import UIKit
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = UINavigationBar.appearance().translucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
hideTransparentNavigationBar()
应该将其重置。
viewDidHide
在父视图控制器中调用它。
艾伦忘了一条线
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
所以我有:
[self.navigationController.navigationBar setTranslucent:YES];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
@Zorayr的好答案已修改为Swift 3:
import Foundation
import UIKit
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), for:.default)
navigationBar.isTranslucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default), for:.default)
navigationBar.isTranslucent = UINavigationBar.appearance().isTranslucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
Swift 4.2和iOS 12
事实证明,您真正需要的只是下面的代码。放入时它完美地工作viewDidLoad()
。
// removes line at bottom of navigation bar
navigationController?.navigationBar.shadowImage = UIImage()
// makes navigation bar completely transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
使用UINavigationBar + Addition窗格,然后只需调用:
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar makeTransparent];
self.edgesForExtendedLayout = UIRectEdgeNone;