2015-07-20 20 views
7

böyle bir arama çubuğu oluşturmak çalışıyorum:UISearchBar özel köşeler

enter image description here

Ama muhtemelen benim kendi suretinde çünkü ile arama çubuğunu değiştirmek zorunda gidiyorum olduğunu fark ediyorum

self.searchController.searchBar.layer.cornerRadius = 50 // I've tried other numbers besides this too with no luck 
self.searchController.searchBar.clipsToBounds = true 

enter image description here

bu ayarlarsanız:

Ben ayarladığınızda arama çubuğu köşeleri dışarı yanlış geliyor
self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.bounds.height/2 

arama çubuğu böyle çıkıyor: hala resimdeki gibi kesin değildir

enter image description here

.

Metin alanının sol ve sağ tarafını, özel arama çubuğumdaki yuvarlatılmış köşeleri kullanabileceğim bir görüntü ile değiştirmenin bir yolu var mı?

cevap

1

Bu hızlı 3 iOS 10'da benim için çalışıyor IS:

searchController.searchBar.layer.cornerRadius = 20 
    searchController.searchBar.clipsToBounds = true 

enter image description here

1

Bu kodu UISearchBar kullanıyorum ancak bu kodu UISearchController ile kullanabilirsiniz.

let searchBar = UISearchBar() 
       searchBar.sizeToFit() 
       searchBar.placeholder = "Search" 
       navigationItem.titleView = searchBar 

       if let textfield = searchBar.value(forKey: "searchField") as? UITextField { 
        textfield.textColor = UIColor.blue 
        if let backgroundview = textfield.subviews.first { 
         // Background color 
         backgroundview.backgroundColor = UIColor.white 
         // Rounded corner 
         backgroundview.layer.cornerRadius = 14; 
         backgroundview.clipsToBounds = true; 
        } 
       }