2014-09-18 3 views
8

UISearchController에 대한 설명서에서는 컨트롤러에 사용할 UISearchBar의 사용자 지정 하위 클래스를 제공하기 위해 - searchBar을 재정의 할 수 있다고 말합니다. 사용자 지정 검색 막대가 사용되며 자체 대리자 메서드가 올바르게 호출되지만 검색 막대가 변경되면 UISearchResultsUpdating 메서드가 더 이상 호출되지 않습니다. 수동으로 많은 배선 작업을 수행해야합니까, 아니면 기본적으로 제공되는 검색 표시 줄과 같이 컨트롤러가 작동하도록하기 위해 누락 된 부분이 있습니까?UISearchController가있는 사용자 지정 UISearchBar

+0

내가 볼 수있는 하위 클래스의 UISearchController를 사용하는 것은 없습니다. – farski

+0

이 문제점에 대한 업데이트가 있습니까? 나는 그와 고투하고있다. 나는 커스텀'UISearchBar'와'UISearchController'를 사용하여 Cancel 버튼이 나타나지 않게했습니다. 이제는 UISearchResultsUpdating이 검색 텍스트 변경시 호출되지 않으므로 결과 테이블이 전혀 표시되지 않습니다. – artooras

답변

3

이것은 알려진 버그입니다. 불행히도 개인 API가 포함되지 않은 해결 방법은 없습니다.

+0

이것에 대해 자세히 설명해 주시겠습니까? 알려진 버그로이 문제에 대한 언급을 찾을 수 없습니다. – pbuchheit

+0

@Alex UISearchController는 iOS 8 이상에서만 사용할 수 있습니다. 컴파일러는 iOS 7 이하 버전에서 클래스를 인식하지 못합니다. iOS 8 이상 만 지원할 때까지는 UISearchDisplayController를 사용하십시오. –

+0

@ MyztikJenz,이 버그와 관련하여 변경된 사항이 있습니까? 내 문제는'UISearchResultsUpdating'이 커스텀'UISearchBar'에 의한 검색 텍스트 변경에서 호출되지 않기 때문에 검색 결과 테이블이 모두 나타나지 않는다는 것입니다. – artooras

0

나는 그것이 그렇게 행동해야한다고 생각합니다.

은 UISearchController.h

// You are free to become the search bar's delegate to monitor for text changes and button presses. 
@property (nonatomic, retain, readonly) UISearchBar *searchBar; 

에서 모든 것을 위임 방법 (updateSearchResultsForSearchController : 당신은 그것의 검색 창에 액세스 할 수 있도록 검색 컨트롤러를 돌려 주어 않습니다.

사용자 설정 검색 막대 위임 방법을 사용하면됩니다.

2

UISearchController를 하위 클래스로 만들 때 getter에서 UISearchBar를 사용자 정의 할 수 있습니다 (setter는 존재하지 않음).

예 - 서브 클래스 구현이 누군가를하는 데 도움이

-(UISearchBar*)searchBar{ 
    UISearchBar *baseSearchBar = [super searchBar]; 
    if (baseSearchBar.showsScopeBar) { 
     baseSearchBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 88); 
    }else{ 
     baseSearchBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44); 
    } 
    return baseSearchBar; 
} 

희망.

+0

그러나 그것은 나를 많이 도왔습니다. –

3

사용자 지정 UISearchController 클래스에서 SearchBar getter를 재정 의하여 사용자 지정 SearchBar를 반환해야하며 이미 초기화해야합니다. 그런 다음 UISearchController init 다음에 해당 속성 만 설정하면 모든 UISearchController 기능이 유지됩니다.

public class DSearchController: UISearchController { 

    private var customSearchBar = DSearchBar() 
    override public var searchBar: UISearchBar { 
     get { 
      return customSearchBar 
     } 
    } 

    required public init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
    } 
    public init(searchResultsController: UIViewController?, 
       searchResultsUpdater: UISearchResultsUpdating?, 
       delegate: UISearchControllerDelegate?, 
       dimsBackgroundDuringPresentation: Bool, 
       hidesNavigationBarDuringPresentation: Bool, 
       searchBarDelegate: UISearchBarDelegate?, 
       searchBarFrame: CGRect?, 
       searchBarStyle: UISearchBarStyle, 
       searchBarPlaceHolder: String, 
       searchBarFont: UIFont?, 
       searchBarTextColor: UIColor?, 
       searchBarBarTintColor: UIColor?, // Bar background 
       searchBarTintColor: UIColor) { // Cursor and bottom line 

     super.init(searchResultsController: searchResultsController) 

     self.searchResultsUpdater = searchResultsUpdater 
     self.delegate = delegate 
     self.dimsBackgroundDuringPresentation = dimsBackgroundDuringPresentation 
     self.hidesNavigationBarDuringPresentation = hidesNavigationBarDuringPresentation   

     customSearchBar.setUp(searchBarDelegate, 
           frame: searchBarFrame, 
           barStyle: searchBarStyle, 
           placeholder: searchBarPlaceHolder, 
           font: searchBarFont, 
           textColor: searchBarTextColor, 
           barTintColor: searchBarBarTintColor, 
           tintColor: searchBarTintColor) 

    } 
} 

그리고 이것은 내 사용자 지정 검색 창입니다 :

public class DSearchBar: UISearchBar { 

    var preferredFont: UIFont? 
    var preferredTextColor: UIColor? 

    init(){ 
     super.init(frame: CGRect.zero) 
    } 

    func setUp(delegate: UISearchBarDelegate?, 
       frame: CGRect?, 
       barStyle: UISearchBarStyle, 
       placeholder: String, 
       font: UIFont?, 
       textColor: UIColor?, 
       barTintColor: UIColor?, 
       tintColor: UIColor?) { 

     self.delegate = delegate 
     self.frame = frame ?? self.frame 
     self.searchBarStyle = searchBarStyle 
     self.placeholder = placeholder 
     self.preferredFont = font 
     self.preferredTextColor = textColor 
     self.barTintColor = barTintColor ?? self.barTintColor 
     self.tintColor = tintColor ?? self.tintColor 
     self.bottomLineColor = tintColor ?? UIColor.clearColor() 

     sizeToFit() 

     //  translucent = false 
     //  showsBookmarkButton = false 
     //  showsCancelButton = true 
     //  setShowsCancelButton(false, animated: false) 
     //  customSearchBar.backgroundImage = UIImage() 
    } 

    required public init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 


    let bottomLine = CAShapeLayer() 
    var bottomLineColor = UIColor.clearColor() 

    override public func layoutSubviews() { 
     super.layoutSubviews() 

     for view in subviews { 
      if let searchField = view as? UITextField { setSearchFieldAppearance(searchField); break } 
      else { 
       for sView in view.subviews { 
        if let searchField = sView as? UITextField { setSearchFieldAppearance(searchField); break } 
       } 
      } 
     } 

     bottomLine.path = UIBezierPath(rect: CGRectMake(0.0, frame.size.height - 1, frame.size.width, 1.0)).CGPath 
     bottomLine.fillColor = bottomLineColor.CGColor 
     layer.addSublayer(bottomLine) 
    } 

    func setSearchFieldAppearance(searchField: UITextField) { 
     searchField.frame = CGRectMake(5.0, 5.0, frame.size.width - 10.0, frame.size.height - 10.0) 
     searchField.font = preferredFont ?? searchField.font 
     searchField.textColor = preferredTextColor ?? searchField.textColor 
     //searchField.backgroundColor = UIColor.clearColor() 
     //backgroundImage = UIImage() 
    } 

} 

초기화 예 :

searchController = DSearchController(searchResultsController: ls, 
            searchResultsUpdater: self, 
            delegate: self, 
            dimsBackgroundDuringPresentation: true, 
            hidesNavigationBarDuringPresentation: true, 
            searchBarDelegate: ls, 
            searchBarFrame: CGRectMake(0.0, 0.0, SCREEN_WIDTH, 44.0), 
            searchBarStyle: .Minimal, 
            searchBarPlaceHolder: NSLocalizedString("Search a location...", comment: ""), 
            searchBarFont: nil, 
            searchBarTextColor: nil, 
            searchBarBarTintColor: UIColor.whiteColor(), 
            searchBarTintColor: iconsColor) 
searchController.searchBar.keyboardAppearance = .Dark 
definesPresentationContext = true 
tableView.tableHeaderView = searchController.searchBar