2017-10-06 9 views
0

Des variantes de cette question ont déjà été posées, mais aucune de ces solutions n'a fonctionné pour moi. J'ai fait à la tableViewHeader le searchBar et c'est ce que j'ai jusqu'ici.Recherche personnaliséeBarre avec bordure

Mon code ressemble à ceci:

override func viewDidLoad() { 
    super.viewDidLoad() 
    configureSearchBar() 
    self.tableView.separatorColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0) 
    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem 
} 

func configureSearchBar() { 
    searchController.searchBar.delegate = self 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.searchBar.placeholder = "Search Companies" 
    definesPresentationContext = true 
    tableView.tableHeaderView = searchController.searchBar 
    searchController.searchBar.barTintColor = UIColor.white 

    //makes border for search box and gives color and rounded 
    //searchController.searchBar.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
    //searchController.searchBar.layer.borderWidth = 2.0 
    //searchController.searchBar.layer.cornerRadius = 15.0 

} 

Et je suis en train de construire cela, mais jusqu'à présent j'ai échoué

What I am trying to build

Si j'essaie ceci:

searchController.searchBar.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
    searchController.searchBar.layer.borderWidth = 1.0 
    searchController.searchBar.layer.cornerRadius = 15.0 
    searchController.searchBar.clipsToBounds = true 

Ça finit par ressembler à ceci, c'est exactement ce que je cherche.

What it ends up looking like

Merci pour votre aide à l'avance

+0

Vous avez plus de chances d'obtenir une réponse utile si vous publiez votre code actuel. – nathan

+0

Copie possible de [Comment changer la bordure et les icônes dans la barre de recherche (Swift)] (https://stackoverflow.com/questions/31413127/how-to-change-border-and-icons-in-search-bar-swift – arvidurs

Répondre

0

I était capable de le faire fonctionner comme ça. Au lieu de changer la taille de l'en-tête, ce qui devait être changé était la taille de la zone de texte dans la barre de recherche.

func configureSearchBarTextField() { 
    for subView in searchController.searchBar.subviews { 
     for subsubView in subView.subviews { 
      if let textField = subsubView as? UITextField { 
       var bounds: CGRect 
       bounds = textField.frame 
       bounds.size.height = 35 //(set height whatever you want) 
       textField.bounds = bounds 
       textField.layer.cornerRadius = 5 
       textField.layer.borderWidth = 1.0 
       textField.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
       textField.backgroundColor = UIColor.white 
      } 
     } 
    } 
} 
0

Cela fonctionne pour moi

self.searchBar.layer.borderColor = UIColor.cyan.cgColor 
self.searchBar.layer.borderWidth = 1 
self.searchBar.layer.cornerRadius = 5.0 
self.searchBar.clipsToBounds = true 

et le délégué à jour la hauteur de l'en-tête:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 200 
} 
+0

Cela a donné à la barre de recherche une bordure avec des coins arrondis mais elle reste de la même taille que l'une des cellules et les bordures supérieure et inférieure sont coupées en haut et en bas de la cellule. Y at-il un moyen de changer la hauteur qui pourrait me donner ce que je veux? –

+0

Il suffit de changer la hauteur de l'en-tête alors. – arvidurs

+0

J'ai essayé de le faire, mais je ne peux pas le comprendre. Je suis un débutant, comment je fais ça? –