代码之家  ›  专栏  ›  技术社区  ›  el-flor

iOS-UISearchController-定义PresentationContext布局问题

  •  4
  • el-flor  · 技术社区  · 10 年前

    我有一个 UIViewController 使用导航栏 HIDDEN ,顶部有几个按钮 UITableView 用一个 UISearchController 作为标题。 问题是:当我创建 UI搜索控制器 ,我还有这行:

        self.definesPresentationContext = YES;
    

    现在,当我使用 UI搜索控制器 ,然后单击中的一个结果 UI表视图 ,它打开了我的关注 UIView控制器 (这正是它应该做的——以及以下内容 UIView控制器 也隐藏了导航栏),但它在顶部显示灰色导航栏 UIView控制器 ,尽管我将导航栏设置为隐藏。

    现在,当我设置:

        self.definesPresentationContext = NO;
    

    导航栏不会出现在下面的视图中,而是显示 UISearchController's SearchBar 出现在下面 UIView控制器 在主视图控制器中的同一位置,尽管它显然不应该在那里了。

    这就是它应该看起来的样子(UIViewController的顶部): enter image description here

    这就是当“自我”时发生的事情。definesPresentationContext=是;

    enter image description here

    这就是当“自我”。definesPresentationContext=否;

    enter image description here

    我如何才能回到第一种情况? 更新 下面是一个重复此问题的示例项目: http://www.filedropper.com/sampleprojectbugreport

    2 回复  |  直到 10 年前
        1
  •  2
  •   Vinny Coyne    10 年前

    它看起来确实像iOS中的一个bug。如果您根本不打算显示导航栏,可以将 UINavigationController 并覆盖 -setNavigationBarHidden:animated: 方法和硬代码 hidden 值:

    -(void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
        [super setNavigationBarHidden:YES animated:animated];
    }
    

    我已经测试了这个解决方法,它阻止导航栏显示。

    请参见更新的示例项目: http://appsandwich.com/stackoverflow/navcontrollersubclass.zip

        2
  •  0
  •   Liam    9 年前

    我也有同样的问题。我用KVO解决了这个问题。 我在视图中发现WillAppear(_:),导航栏 isHidden 是真的。但在视图DidAppear(_:)中,导航栏 是隐藏的 更改为false。我不知道发生了什么。但我想我可以用KVO捕捉并改变它。

    首先,在视图中添加观察者WillAppear(_:)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.addObserver(self, forKeyPath: "hidden", options: [.new], context: nil)
    }
    

    然后,更改导航栏 是隐藏的 在observerValue()中

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        guard let keyPath = keyPath else { return }    
        if keyPath == "hidden" {
            // you should remove the KVO before set
            navigationController?.navigationBar.removeObserver(self, forKeyPath: "hidden")
            navigationController?.navigationBar.isHidden = true
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    

    现在导航栏已按预期隐藏。 希望有帮助。