Swift:SearchBar上的黑色/灰色边框在点击

时间:2017-09-01 17:46:16

标签: ios swift border uisearchbar

使用Swift 3并在我的设备上进行测试,我尝试了几个代码,应该删除黑色/灰色边框,但它没有被删除。奇怪的是边框就在那里,但是一旦我点击了准备输入的searchBar,边框就不再存在了,直到再次加载视图。所以边框只显示,直到我点击searchBar。

这是我的代码:

// Coloring TableView
myTableView.backgroundColor = UIColor.white
myTableView.sectionIndexBackgroundColor = UIColor.black
myTableView.sectionIndexColor = UIColor.black

// Search Bar
searchController.searchBar.backgroundColor = UIColor.white
searchController.searchBar.barTintColor = UIColor.white

// Coloring SearchBar Cancel button
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.black]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)

// Scope: Selected text
let titleTextAttributesSelected = [NSForegroundColorAttributeName: UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesSelected, for: .selected)

// Scope: Normal text
let titleTextAttributesNormal = [NSForegroundColorAttributeName: UIColor.black]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesNormal, for: .normal)

// Coloring Scope Bar
UISegmentedControl.appearance().tintColor = UIColor.black
UISegmentedControl.appearance().backgroundColor = UIColor.white

// Search Bar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
myTableView.tableHeaderView = searchController.searchBar

// Scope Bar
searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Open Beta"]
searchController.searchBar.delegate = self

let searchController = UISearchController(searchResultsController: nil)

// SEARCH BAR: Filtering Content
func filterContentForSearchText(searchText: String, scope: String = "All") {

    filteredFollowedArray = followedArray.filter { Blog in

        let categoryMatch = (scope == "All") || (Blog.blogType == scope)

        return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased()))
    }

    filteredBlogArray = blogArray.filter { Blog in

        let categoryMatch = (scope == "All") || (Blog.blogType == scope)

        return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased()))
    }

    myTableView.reloadData()
}

// SEARCH BAR: Updating Results
func updateSearchResults(for searchController: UISearchController) {

    filterContentForSearchText(searchText: searchController.searchBar.text!)
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {}

// SEARCH BAR: Scope
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {

    filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}

// SEARCH BAR: Updating Scope
func updateSearchResultsForSearchController(searchController: UISearchController) {

    let searchBar = searchController.searchBar
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]

    filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope)
}

// Deallocating Search Bar
deinit{
    if let superView = searchController.view.superview {
        superView.removeFromSuperview()
    }
}

这是首次加载视图时searchBar的外观,顶部的线是灰色的(我不知道为什么灰色看起来如此褪色,你看不到它,但是一条非常细的灰色线是有):

pic1

单击searchBar后,该行变为黑色:

pic2

当我添加这个引用给我的代码时会发生这种情况:

// Search Bar Border
let searchBar = searchController.searchBar
searchBar.backgroundImage = UIImage()

pic3

这是在我点击带有该代码的searchBar之后,以及它应该如何看待。没有边界:

pic4

1 个答案:

答案 0 :(得分:0)

您是否只在小型模拟器中检查此行为?第二个屏幕截图中的黑线在第一个屏幕截图中看起来并未与您的搜索栏相关联。它看起来像状态栏的底部。有时模拟器无法正确显示细线。我会将模拟器的大小增加到最大或更好,然后在手机上进行测试。