Navigation bar + Search controller + Large title:

2019-08-25 18:59发布

问题:

Context:

  • UINavigationController with a UITableViewController
  • UISearchController in the navigation bar
  • Navigation bar translucent with black style, it uses large title, tintColor, barTintColor (so no background image).

Issue:

I have a strange animation glitch that shows a hairline above the search bar. The hairline appears only during the scroll.

I have already tried many solutions concerning similar problems, but they have not helped.

Tested with an iPhone 7 with iOS 12.1.3

回答1:

You can fix hairline issue using this

searchController.searchBar.layer.borderColor = UIColor(red: 242/255.0, green: 82/255.0, blue: 46/255.0, alpha: 1).CGColor
searchController.searchBar.layer.borderWidth = 1

if the above didn’t work. you can remove the hairline completely

extension UINavigationBar {
        func hideBottomHairline() {
            self.hairlineImageView?.isHidden = true
        }

        func showBottomHairline() {
            self.hairlineImageView?.isHidden = false
        }
    }

extension UIView {
    fileprivate var hairlineImageView: UIImageView? {
        return hairlineImageView(in: self)
    }

    fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
        if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
            return imageView
        }

        for subview in view.subviews {
            if let imageView = self.hairlineImageView(in: subview) { return imageView }
        }

        return nil
    }
}