2016-10-11 30 views
8

Harici bir API'dan öğe katalogu aramak için UISearchBar uyguladım. Arama işlevi beklendiği gibi çalışır, ancak sorun, arama çubuğu metin alanının sağ tarafındaki iptal düğmesine bastığımda, tüm arama çubuğunun bir satır aşağı inmesi ve tümüyle iter gibi görünmesidir. masa görünümü de aşağı.Arama çubuğu her seferinde bir satır aşağı atlıyor İptal düğmesine dokunuldu

Arama çubuğunun metin alanına bir harf yazıp ardından iptal tuşuna basarsanız, arama çubuğu metin alanı satır yüksekliği olan 44 piksel kısaltılır ve tablo görünümü de aynı miktarda azaltılır. . Sürekli olarak bir şeyler yazıp tuşuna basarsanız, iptal et tuşuna basarsanız, arama çubuğu daha ileriye ve aşağıya doğru ilerler. Herhangi bir tavsiye harika olurdu! viewDidLoad yılında configureSearchController() aramaya

import Foundation 
import UIKit 
import ItemLogger 


private extension Selector { 
    static let dismiss = #selector(SearchVC.dismissView) 
} 


extension SearchVC: UISearchResultsUpdating { 
    func updateSearchResultsForSearchController(searchController: UISearchController) { 
     let searchBar = searchController.searchBar 
     let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] 
     filterContentForSearchText(searchController.searchBar.text!, scope: scope) 
    } 
} 
extension SearchVC: UISearchBarDelegate { 
    func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 
     filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) 
    } 
} 

class SearchVC: UITableViewController { 

    let searchController = UISearchController(searchResultsController: nil) 
    var searchedItems = [ItemLog]() 
    var searchedImages = [UIImage]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back_Button"), style: UIBarButtonItemStyle.Plain, target: self, action: .dismiss) 
     self.navigationItem.leftBarButtonItem = leftBarButtonItem 
    } 


    override func viewWillAppear(animated: Bool) { 
     configureSearchController() 
    } 


    override func prefersStatusBarHidden() -> Bool { 
     return true 
    } 


    func configureSearchController() { 

     guard !searchController.active else { 
      return 
     } 

     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = false 
     searchController.searchBar.placeholder = "Type to Search" 

     definesPresentationContext = true 
     searchController.searchBar.scopeButtonTitles = ["All"] 
     searchController.searchBar.delegate = self 
     searchController.searchBar.sizeToFit() 
     tableView.tableHeaderView = searchController.searchBar 

     let view: UIView = self.searchController.searchBar.subviews[0] as UIView 
     for subView: UIView in view.subviews { 
      if let textView = subView as? UITextField { 
       textView.tintColor = UIColor.orangeColor() 
       textView.textColor = UIColor.blackColor() 
       textView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.05) 
      } 
     } 
     searchController.searchBar.barTintColor = UIColor.whiteColor() 

     let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.33)] 
     UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal) 
    } 



    func searchBarTextDidBeginEditing(searchBar: UISearchBar) { 
     tableView.reloadData() 
    } 


    override func tableView(tableView:UITableView, numberOfRowsInSection section: Int) -> Int { 
     if searchController.active && searchController.searchBar.text != "" { 
      return searchedItems.count 
     } 

     return 0 

    } 

    override func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = self.tableView.dequeueReusableCellWithIdentifier("items", forIndexPath: indexPath) 

     let label = cell.viewWithTag(111) as! UILabel 
     let nameLabel = cell.viewWithTag(222) as! UILabel 
     let art = cell.viewWithTag(333) as! UIImageView 

     if searchController.active && searchController.searchBar.text != "" && searchController.searchBar.text != NSCharacterSet.whitespaceCharacterSet(){ 

      label.text = searchedItems[indexPath.row].title 
      nameLabel.text = searchedItems[indexPath.row].name 
      art.image = searchedImages[indexPath.row] 
     } 
     return cell 
    } 



    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     print(searchedItems[indexPath.row]) 
     self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 


    func filterContentForSearchText(searchText: String, scope: String = "All") { 


     if searchController.active && searchController.searchBar.text != "" && searchController.searchBar.text != NSCharacterSet.whitespaceCharacterSet() { 
      let queries: [SearchQueryOptions] = [ 
       .QueryString(searchController.searchBar.text!)] 
      ItemLog.search(queries, completion: { (result) in 
       if let itms = result.response.result where itms.count > 0 { 
        self.searchedItems.removeAll() 
        self.searchedImages.removeAll() 
        for i in 0...itms.count - 1 { 

         self.searchedItems.append(itms[i]) 
         self.searchedImages.append(itms[i].img) 

        } 
       } 
       self.tableView.reloadData() 
      }) 
     } 
    } 


    func dismissView(){ 
     self.navigationController?.popToRootViewControllerAnimated(true) 
    } 

} 
+0

Sorunuzu storyboard'unuzla ve çalışma zamanı sonucunuzla güncelleyin. –

+0

@MikeG Cevabıma bak ... – Joe

cevap

7

Kod Not

Swift 3. test açık kaynak projeyi yaptık: Ben, kodunuzu çalıştığınızda. Aynı konuyla yüzleşiyordum. Her nasılsa,

class SearchVC: UITableViewController,UISearchBarDelegate,UISearchResultUpdating { 

var resultSearchController = UISearchController() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    configureSearchController() 
} 


override var prefersStatusBarHidden: Bool { 

    return true 
} 


func configureSearchController() { 

    self.resultSearchController = ({ 
     let controller = UISearchController(searchResultsController: nil) 
     controller.searchResultsUpdater = self 
     controller.dimsBackgroundDuringPresentation = false 
     controller.hidesNavigationBarDuringPresentation = false 
     controller.searchBar.searchBarStyle = .default 
     controller.searchBar.sizeToFit() 
     controller.searchBar.setShowsCancelButton(false, animated: true) 
     controller.searchBar.keyboardAppearance = .default 

     self.tableView.tableHeaderView = controller.searchBar 

     //controller.searchBar.tintColor = UIColor(patternImage: UIImage(named: "xxxx")!) 
     // controller.searchBar.setBackgroundImage(UIImage(named: "xxxx"), forBarPosition: UIBarPosition.Top, barMetrics: UIBarMetrics.Default) 
     // controller.searchBar.backgroundImage = UIImage(named: "xxxx") 
     // controller.searchBar.setImage(UIImage(named: "search-icon.png"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal) 

     return controller 
    })() 


    for subView in self.resultSearchController.searchBar.subviews 
    { 
     for subsubView in subView.subviews 
     { 
      if let textField = subsubView as? UITextField 
      { 
       textField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search Text", comment: ""), attributes: [NSForegroundColorAttributeName: UIColor.red]) 

       textField.adjustsFontSizeToFitWidth = true 
       textField.allowsEditingTextAttributes = true 


       textField.textColor = UIColor.red 
       textField.layer.borderColor = UIColor.gray.cgColor 
       textField.layer.cornerRadius = 5 
       textField.layer.masksToBounds = true 

       textField.layer.borderWidth = 0.215 

      } 
     } 
     } 
    } 
} 

Güncelleme etrafında ... başardı: .... code..hope yukarıdan

func updateSearchResults(for searchController: UISearchController) {} 

Çıktı, cevabım sorununuzu çözecektir enter image description here

+0

Awesome! Biraz test ettikten sonra, aramanın searchBar kapsamıyla bir şey olduğunu farkettim. Hala tam olarak ne olduğundan emin değilsiniz, fakat cevabınızın ve bazı tinkering'in bir birleşimi hile yaptı. Çok teşekkürler – MikeG

0

Dene: İşte benim kodudur. viewWillAppear numaranızda super.viewWillAppear(animated:) numaralı telefonu aramayı unutmayın.

+0

Ah iyi düşünceler! Maalesef hala aynı sorunu yaşıyor – MikeG

0

Ben SearchTableView

self.searchController.searchBar.sizeToFit() 
self.tableHeaderView = self.searchController.searchBar 

searchTableView.layoutMargins = UIEdgeInsets.zero 
definesPresentationContext = true 
extendedLayoutIncludesOpaqueBars = true