Swift:SearchBar上的黑色/灰色边框不会消失,直到点击

问题描述:

使用Swift 3并在我的设备上进行测试时,我尝试了几个应该删除黑色/灰色边框但未被删除的代码。奇怪的是边框在那里,但是一旦我点击searchBar准备键入,边框不再存在,直到再次加载视图。所以边框只显示直到我点击searchBar。Swift: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() 
    } 
} 

这是搜索栏的外观首次加载视图时,顶部的线是灰色的(我不为什么这里的灰色看起来如此变淡,则还看不出来,但一个非常薄的灰色的线有):

pic1

,你点击搜索栏后,该行变为黑色:

pic2

而这发生在我加入这样的代码,其被称为我:

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

pic3

,这是在我点击与该代码搜索栏,以及它应该如何看。无边框:

pic4

你检查只在小型模拟器这种行为?第二个屏幕截图中的黑线在第一个屏幕截图中看起来没有连接到搜索栏。它看起来像是状态栏的底部。有时模拟器无法正确显示细线。我会将模拟器尺寸增加到最大或更好,然后在手机上测试它。

+0

这一切都在我的设备上,iPhone 6s – BroSimple