删除导航栏和工具栏的边框

时间:2019-04-14 16:44:45

标签: ios swift

我在删除UISearchBar,UINavigationBar和UIToolBar的边框阴影时遇到麻烦。因此,基本上,我有一个视图控制器,其中嵌入了一个UISearchController,下面是一个具有UISegmentedControl的UIToolbar。问题在于,即使在单独使用外观()委托删除UINavigationBar,UISearchBar,UIToolbar的边框阴影之后,边框仍会显示在UISearchBar和UIToolBar之间。

当我转到下一个屏幕时,我发现UINavigationBar没有边框阴影,因此,可以确定显示的边框不是UINavigationBar。

我还发现了一件奇怪的事情,当我从UINavigationBar隐藏UISearchController时,UINavigationBar和UIToolBar之间没有边界。当我隐藏UIToolBar时,UINavigationBar之后也没有边框。

extension AppDelegate {

    private func appearanceSetup() {
        UINavigationBar.appearance().barTintColor = .appBlueColor
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        UITextField.appearance().tintColor = .appBlueColor
        UISearchBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        UISearchBar.appearance().barTintColor = .appBlueColor
        UISearchBar.appearance().backgroundImage = UIImage()
        UIToolbar.appearance().setShadowImage(UIImage(), forToolbarPosition: .any)
        UIToolbar.appearance().barTintColor = .appBlueColor
        UIToolbar.appearance().layer.borderWidth = 1
        UIToolbar.appearance().layer.borderColor = UIColor.appBlueColor.cgColor
    }
}
class HomeViewController: UIViewController {
   private let searchController: UISearchController = {
        let searchController = UISearchController(searchResultsController: nil)
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search"
        searchController.searchBar.tintColor = .white
        searchController.searchBar.barTintColor = .appBlueColor
        return searchController
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.searchController = searchController
    }

Remove border between Search & ToolBar Example -> No Border after SearchBar Example -> No Border between Nav & ToolBar

4 个答案:

答案 0 :(得分:1)

self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

尝试此代码,希望它能解决您的问题

答案 1 :(得分:0)

一旦我遇到类似的问题,appeareance并没有解决我的问题。因此,我开始自定义组件。我为UINavigationBar进行了此操作,尽管我的UINavigationBar放在情节提要中,没有代码,但仍然可以正常工作,尽管它可以工作。试试这个,让我知道:

class CustomNavigationBar: UINavigationBar
{
    override init(frame: CGRect) {
        super.init(frame: frame)
        setBackgroundImage(UIImage(), for: .default)
        shadowImage = UIImage()
        backgroundColor = <Your UIView.backgoundColor of type: UIColor>
        tintColor = UIColor.white
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setBackgroundImage(UIImage(), for: .default)
        shadowImage = UIImage()
        backgroundColor = <Your UIView.backgoundColor of type: UIColor>
        tintColor = UIColor.white
    }
}

我不知道您对UISearchController的态度是什么,但是我在searchBar中也遇到了同样的问题,因此我在代码中添加了searchBar并使用了委托。占位符出现两次,因为iOS版本,只有一个版本在iOS 10或iOS 10+以下无法正常工作。取决于将要删除的那个

    override func viewDidLoad() {
        super.viewDidLoad()

        setup()
        installRefresh()
    }

    private func setup() {
        let searchBar = UISearchBar()
        searchBar.scopeButtonTitles = [placeholdetText] // The String for the place holder
        searchBar.searchBarStyle = .default
        searchBar.barTintColor = view.backgroundColor 
        searchBar.placeholder = placeholdetText + ".."
        searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        searchBar.backgroundColor = UIColor.clear
        searchBar.delegate = self

        // Extra UI Setup
   }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty
        {
            // The filters objects it's what i show after the search, and the 
            // general objects it's all the objects
            filtersObjects = generalObjects
        }
        else
        {
           //... Al the code tu filter from the searchText 
        }
          // Refresh the UI
    }

答案 2 :(得分:0)

bin/logstash -f config2.conf

答案 3 :(得分:0)

覆盖func viewDidLoad(){         super.viewDidLoad()

    //MARK:- for default navigation bar

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    self.navigationItem.backBarButtonItem?.tintColor = UIColor.white

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.layoutIfNeeded()

}