Swift How remove searchController bottom border?
Asked Answered
S

2

0

This page has tableView and SearchController. My problem is searchController borders. 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

How to hide UINavigationBar 1px bottom line

How can I remove border bottom of UINavigationBar?

enter image description here

           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?

Schleswigholstein answered 13/7, 2019 at 11:35 Comment(3)
This is navigation bar separator View instead of SearchBar. Check againBadgett
try this one : let navigationBar = navigationController!.navigationBar navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"), for: .default) navigationBar.shadowImage = UIImage()Badgett
Thanks for answer. when you remove it SearchController getting lost line. Your code outputs: imgur.com/nQ8ogmxSchleswigholstein
B
1

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

and Add these two line

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.

Badgett answered 13/7, 2019 at 15:38 Comment(6)
Thanks for answer @rmaddy. I did this and outputs still same. Your code imgur.com/GqljhkG, output : imgur.com/nQ8ogmxSchleswigholstein
@yusufdemirkoparan Not my answer. I just made an edit.Proem
@Proem ah sorry :). Thanks for answers Sumit Garg.Schleswigholstein
check updated Comment, i try this code in sample project and it is working.Badgett
@SumitGarg Hi Sumit you can share me your sample project?Schleswigholstein
here is the sample code: drive.google.com/open?id=1i2IC0SHUaaBIsABXBMbI-q6b3ifQIwXrBadgett
E
0

Create an extension of 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
}
Ehlers answered 26/10, 2020 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.