Swift如何删除searchController底部边框? [英] Swift How remove searchController bottom border?

查看:71
本文介绍了Swift如何删除searchController底部边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页面具有tableView和SearchController.我的问题是searchController边框.我无法删除边框

我尝试过:

 如果#available(iOS 11.0,*){让scb = searchController.searchBarscb.tintColor = UIColor.whitescb.barTintColor = UIColor.whitescb.layer.masksToBounds = truescb.layer.borderWidth = 10scb.layer.borderColor = UIColor.clear.cgColor如果让textfield = scb.value(forKey:"searchField")为?UITextField {textfield.layer.borderWidth = 2textfield.layer.borderColor = UIColor.clear.cgColor//textfield.textColor =//设置文字颜色如果让backgroundview = textfield.subviews.first {//背景颜色backgroundview.backgroundColor = UIColor.whitebackgroundview.layer.borderWidth = 0backgroundview.layer.borderColor = UIColor.clear.cgColor//圆角backgroundview.layer.cornerRadius = 10;backgroundview.clipsToBounds = true;}}如果让navigationbar = self.navigationController?.navigationBar {navigationbar.barTintColor = UIColor.clear}navigationItem.hidesSearchBarWhenScrolling = false}navigationItem.searchController = searchControllernavigationItem.largeTitleDisplayMode = .neverself.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont.systemFont(ofSize:18,weight:.bold)]searchController.obscuresBackgroundDuringPresentation =否searchController.searchBar.placeholder =搜索糖果"navigationItem.searchController = searchControllerdefinePresentationContext = true 

如何解决此问题?有想法吗?

解决方案

一种解决方案是将导航栏的背景图像和阴影图像设置为空图像.

我又做了一个更改,只是发表评论

navigationItem.largeTitleDisplayMode =.从不

并添加这两行

navigationbar.setBackgroundImage(UIImage(),用于:.default)navigationbar.shadowImage = UIImage()

这是完整的代码:

 如果#available(iOS 11.0,*){让scb = searchController.searchBarscb.tintColor = UIColor.whitescb.barTintColor = UIColor.whitescb.layer.masksToBounds = truescb.layer.borderWidth = 10scb.layer.borderColor = UIColor.clear.cgColor如果让textfield = scb.value(forKey:"searchField")为?UITextField {textfield.layer.borderWidth = 2textfield.layer.borderColor = UIColor.clear.cgColor//textfield.textColor =//设置文字颜色如果让backgroundview = textfield.subviews.first {//背景颜色backgroundview.backgroundColor = UIColor.whitebackgroundview.layer.borderWidth = 0backgroundview.layer.borderColor = UIColor.clear.cgColor//圆角backgroundview.layer.cornerRadius = 10;backgroundview.clipsToBounds = true;}}如果让navigationbar = self.navigationController?.navigationBar {navigationbar.barTintColor = UIColor.whitenavigationbar.setBackgroundImage(UIImage(),用于:.default)navigationbar.shadowImage = UIImage()}navigationItem.hidesSearchBarWhenScrolling = false}//navigationItem.largeTitleDisplayMode = .neverself.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont.systemFont(ofSize:18,weight:.bold)]searchController.obscuresBackgroundDuringPresentation =否searchController.searchBar.placeholder =搜索糖果"navigationItem.searchController = searchControllerdefinePresentationContext = true覆盖func viewDidAppear(_动画:布尔){super.viewDidAppear(动画)如果让navigationbar = self.navigationController?.navigationBar {navigationbar.setBackgroundImage(UIImage(),用于:.default)navigationbar.shadowImage = UIImage()}} 

请检查更新的代码.

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?

           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

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.

这篇关于Swift如何删除searchController底部边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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