简体   繁体   English

Swift 如何移除searchController 底部边框?

[英]Swift How remove searchController bottom border?

This page has tableView and SearchController.这个页面有 tableView 和 SearchController。 My problem is searchController borders.我的问题是 searchController 边框。 I can't remove borders我无法删除边框

I tried: Remove border between View and Search Bar我试过: 删除视图和搜索栏之间的边框

Remove navigation bar bottom line when using search controller 使用搜索控制器时删除导航栏底线

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

How to hide UINavigationBar 1px bottom line 如何隐藏 UINavigationBar 1px 底线

How can I remove border bottom of UINavigationBar? 如何删除 UINavigationBar 的边框底部?

在此处输入图片说明

           if #available(iOS 11.0, *) {
                   let scb = searchController.searchBar
                   scb.tintColor = UIColor.white
                   scb.barTintColor = UIColor.white
                   scb.layer.masksToBounds = true
                   scb.layer.borderWidth = 10
                   scb.layer.borderColor = UIColor.clear.cgColor

                   if let textfield = scb.value(forKey: "searchField") as? UITextField {
                       textfield.layer.borderWidth = 2
                       textfield.layer.borderColor = UIColor.clear.cgColor
                       //textfield.textColor = // Set text color
                       if let backgroundview = textfield.subviews.first {
                           // Background color
                           backgroundview.backgroundColor = UIColor.white
                           backgroundview.layer.borderWidth = 0

                           backgroundview.layer.borderColor = UIColor.clear.cgColor
                           // Rounded corner
                           backgroundview.layer.cornerRadius = 10;
                           backgroundview.clipsToBounds = true;
                       }
                   }
                   if let navigationbar = self.navigationController?.navigationBar {
                       navigationbar.barTintColor = UIColor.clear
                   }
                               navigationItem.hidesSearchBarWhenScrolling = false

               }
                navigationItem.searchController = searchController

       navigationItem.largeTitleDisplayMode = .never

       self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
       searchController.obscuresBackgroundDuringPresentation = false
       searchController.searchBar.placeholder = "Search Candies"
       navigationItem.searchController = searchController
       definesPresentationContext = true

How fix this issue?如何解决这个问题? Any has idea?有什么想法吗?

One solution is to set the navigation bar's background and shadow images to an empty image.一种解决方案是将导航栏的背景和阴影图像设置为空图像。

i did one more change, just comment我又做了一个改动,只是评论

navigationItem.largeTitleDisplayMode = .never navigationItem.largeTitleDisplayMode = .never

and Add these two line并添加这两行

navigationbar.setBackgroundImage(UIImage(), for: .default) navigationbar.shadowImage = UIImage() navigationbar.setBackgroundImage(UIImage(), for: .default) navigationbar.shadowImage = UIImage()

Here is the complete code :这是完整的代码:

 if #available(iOS 11.0, *) {
            let scb = searchController.searchBar
            scb.tintColor = UIColor.white
            scb.barTintColor = UIColor.white
            scb.layer.masksToBounds = true
            scb.layer.borderWidth = 10
            scb.layer.borderColor = UIColor.clear.cgColor

            if let textfield = scb.value(forKey: "searchField") as? UITextField {
                textfield.layer.borderWidth = 2
                textfield.layer.borderColor = UIColor.clear.cgColor
                //textfield.textColor = // Set text color
                if let backgroundview = textfield.subviews.first {
                    // Background color
                    backgroundview.backgroundColor = UIColor.white
                    backgroundview.layer.borderWidth = 0

                    backgroundview.layer.borderColor = UIColor.clear.cgColor
                    // Rounded corner
                    backgroundview.layer.cornerRadius = 10;
                    backgroundview.clipsToBounds = true;
                }
            }
            if let navigationbar = self.navigationController?.navigationBar {
                navigationbar.barTintColor = UIColor.white
                navigationbar.setBackgroundImage(UIImage(), for: .default)
                navigationbar.shadowImage = UIImage()

            }
            navigationItem.hidesSearchBarWhenScrolling = false
        }

//        navigationItem.largeTitleDisplayMode = .never
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search Candies"
        navigationItem.searchController = searchController
        definesPresentationContext = true



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

    if let navigationbar = self.navigationController?.navigationBar {
        navigationbar.setBackgroundImage(UIImage(), for: .default)
        navigationbar.shadowImage = UIImage()
    }
}

Please check updated code.请检查更新的代码。

Create an extension of UISearchController:创建 UISearchController 的扩展:

extension UISearchController {

var hairlineView: UIView? {
    let barBackgroundView = searchBar.superview?.subviews.first { String(describing: type(of: $0)) == "_UIBarBackground" }
    
    guard let background = barBackgroundView else { return nil }
    return background.subviews.first { $0.bounds.height == 1 / self.traitCollection.displayScale }
    } 
}

Call this in view will layout subview:在视图中调用这个将布局子视图:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.navigationItem.searchController?.hairlineView?.isHidden = true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM