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

时间:2018-11-18 13:49:13

标签: ios swift uinavigationbar uisplitviewcontroller

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

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

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

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

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

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

在此方面的任何帮助都将受到赞赏。

Image of my storyboard

1 个答案:

答案 0 :(得分:1)

对于通用流

您可以使用此设置。说ViewController是所有其他视图控制器类(需要阴影的地方),而DetailViewController是详细信息视图控制器类。

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

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
    }

}

DetailViewController.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()
    }
}

故事板设置 enter image description here

输出

enter image description here

注意:另一种巧妙的方法是将阴影存储在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()
}

在您的 DetailViewController.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
}

输出(主/明细流)

enter image description here