删除大标题UINavigationBar中UISearchController顶部的1px行 [英] Remove 1px line at top of UISearchController in large titles UINavigationBar

查看:94
本文介绍了删除大标题UINavigationBar中UISearchController顶部的1px行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从具有大样式UINavigationItem的视图过渡到具有大样式UINavigationItemUISearchController的视图.我已经自定义了UINavigationBar的背景颜色.

I'm transitioning from a view with a large style UINavigationItem into a view that has a large style UINavigationItem and a UISearchController. I've customized the background colour of the UINavigationBar.

由于某些原因,在UINavigationBarUISearchController之间有1px的线或空格.我可以从NavigationBar看出来,因为如果滚动以使搜索栏停留在顶部,该行就会消失.我只是不确定是什么产生了这一行.除了设置barTintColortintColor之外,我没有使用阴影或其他任何花哨的东西.如果我不使用大样式标题,则仅在视图之间切换时才看到该行.就像转换UISearchController时一样,只是不正确地粘在UINavigationBar上.

For some reason, there's a 1px line or space between the UINavigationBar and the UISearchController. I can tell it's from the NavigationBar, because if I scroll so that the search bar sticks to the top, the line disappears. I'm just not sure what's generating this line. I'm not using shadows, or anything fancy, other than setting the barTintColor and the tintColor. If I don't use large style titles, I see the line, but only when transitioning between views. It's like when transitioning the UISearchController just doesn't stick right to the UINavigationBar.

我们希望能帮助您找出这条线的出处.

Any help in figuring out where this line is coming from is appreciated.

更新:经过一些进一步的调查,看来这仅在navigationItem.hidesSearchBarWhenScrolling设置为false时才会发生.如果最初是隐藏的,则前一个视图中的动画是平滑的,并且显示的条也是平滑的.

Update: On some further investigation, it seems this only happens when navigationItem.hidesSearchBarWhenScrolling is set to false. If it initially is hidden, then the animation from the previous view is smooth, and showing the bar is also smooth.

推荐答案

测试您描述的导航项和搜索栏设置,我只能在过渡动画期间重现1px/点的线.

Testing the navigation item and search bar setup you've described, I could only reproduce the 1px/point line during the transition animations.

解决方法:这行实际上是所使用的背景布局视图(forLastBaselineLayout),由于UISearchBar的错误框架而最终可见.设置背景以匹配导航栏颜色,以将其隐藏在后站点

Workaround: This line is actually a background layout view (forLastBaselineLayout) used by UINavigationBar ending up visible due to the buggy framing of UISearchBar. Set its background to match your navigation bar color to hide it in hind site

// MARK: UINavigationControllerDelegate

public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    viewController.navigationController?.navigationBar
        .forLastBaselineLayout.backgroundColor = .red // TODO:
}

确保将实现UINavigationControllerDelegate的对象设置为navigationController.delegate,以接收上述委托调用.

Make sure to set the object implementing UINavigationControllerDelegate is set as navigationController.delegate to receive the above delegate call.

另外(不是问题的一部分): UISearchBar在控制器之间进行转换(推入和弹出)时表现得很奇怪,尤其是当一个控制器具有/显示一个searchBar而不是另一个时. 解决方法,我发现从显示控制器中暂时隐藏搜索栏在视觉上更令人愉悦.这是我使用的代码:

In addition (not part of the question): UISearchBar seems to behave strangely when transitioning (push and pop) between controllers especially when one has/show a searchBar but not the other. Workaround, I found it visually more pleasing to temporarily hide the search bar from presenting controllers. Here is the code I use:

public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if let sender = navigationStack.popLast() {
        prepareSearchBarForTransition(from: sender)
    }
    navigationStack = navigationController.children
}

/// Set only from `navigationController: willShow`
private var navigationStack: [UIViewController] = []

func prepareSearchBarForTransition(from sender: UIViewController) {
    if #available(iOS 11.0, *) {
        let searchController = sender.navigationItem.searchController
        sender.navigationItem.searchController = nil
        weak var weakSender = sender
        let navigationTransitionDuration: TimeInterval = 0.33
        DispatchQueue.main.asyncAfter(deadline: .now() + navigationTransitionDuration) {
            /// Reset the search bar.
            weakSender?.navigationItem.searchController = searchController
        }
    } else {
        // TODO: Check if the above workaround is neccessary for < iOS 11
    }
}

这篇关于删除大标题UINavigationBar中UISearchController顶部的1px行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆