如何仅在详细视图中隐藏导航栏阴影?

How to hide the navigation bar shadow only in detail view?

SO中有很多答案提供了隐藏导航栏阴影的解决方案。这些对我有用,除了我在这里描述的这个特殊情况。因此,这个问题不是重复的。

为了测试这个特殊案例,我使用主从应用程序模板创建了一个新项目。在 DetailViewController -> viewDidAppear 中,我编写了以下代码:

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

以上代码适用于单视图应用程序和 iPad Air 2 模拟器。但是,它不适用于 iPhoneX 模拟器中主从应用程序的 detailViewController。

或者,我还尝试在 viewDidAppear 中检索导航栏的子视图并尝试隐藏阴影(请参见下面的代码)。但是,子视图计数为零。怎么可能?

    for parent in self.navigationController!.navigationBar.subviews {
        for childView in parent.subviews {
            if(childView is UIImageView) {
                childView.removeFromSuperview()
            }
        }
    }

非常感谢任何帮助。

对于通用流程

您可以使用此设置。说 ViewController 是所有其他视图控制器 class (你想要阴影的地方), DetailViewController 是详细视图控制器 class.

我正在做的事情是保留阴影图像。

ViewController.swift

class ViewController: UIViewController {

    var shadowImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

         shadowImage = self.navigationController?.navigationBar.shadowImage
    }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationController?.navigationBar.shadowImage = shadowImage
    }

}

详情ViewController.swift

class DetailViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
}

故事板设置

输出

注意:另一种巧妙的方法是将阴影存储在 DetailsViewController 中,并在视图即将消失时设置它

class DetailsViewController: UIViewController {

    var shadowImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        shadowImage = self.navigationController?.navigationBar.shadowImage
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.shadowImage = shadowImage
    }
}

这个解决方案更优雅,管理更干净。

对于 MasterDetailFlow,使用 SplitViewController

在你的MasterViewControlelr.swift

override func viewWillAppear(_ animated: Bool) {
    clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed // placeholder code when you created the project
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.shadowImage = nil
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

在你的详情中ViewController.swift

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.navigationBar.shadowImage = nil
}

输出(Master/Detail流量)