簡體   English   中英

當將UISearchBar添加到iOS 7.1上的UITableViewCell時,它會更改其超級視圖

[英]UISearchBar changes its superview when it is added to UITableViewCell on iOS 7.1

在我的項目中,我需要一個UISearchBar才能在UITableView上滾動。 因此,我將其放在這樣的UITableViewCell

    searchBar                   = [[SearchBar alloc] initWithFrame:cell.bounds];
    searchBar.delegate          = self;
    searchBar.placeholder       = NSLocalizedString(@"Search friends", "");
    searchBar.layer.borderWidth = 1.f;
    searchBar.layer.borderColor = [UIColor whiteColor].CGColor;

    sdc = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:nil];
    sdc.delegate = self;

    [cell addSubview:searchBar];

並且它在iOS 6上運行良好。但是在iOS 7上,當焦點對准時,它會切換到另一個超級視圖(並且實際上從屏幕上消失)。 最初,我認為我的項目有問題,導致這種行為,但是后來我創建了一個簡單的測試項目,並驗證了這一點-確實,這是一條規則,在iOS 7.1上,添加到UITableViewCell UISearchBar移至另一個Superview並從中丟失了成為第一響應者后立即顯示。

我在搜索欄上重寫了willMoveToSuperview:方法,我看到它從UITableViewCellScrollView移到一個視圖,該視圖是UIView ,其超級視圖的類是UISearchDisplayControllerContainerView

經過數小時的搜索和實驗,我無法弄清是什么原因導致了這種行為以及如何逃避它。 我唯一確定的是,它恰好發生在以下兩個UISearchDisplayDelegate方法的調用之間: searchDisplayControllerWillBeginSearch:searchDisplayControllerDidBeginSearch:

有沒有人遇到過這個? 任何想法如何解決呢?

您確定需要這種奇怪的方法來將UISearchBar添加到UITableViewCell以便對其進行滾動嗎? 我只是像這樣使用smthng:

UISearchBar *mainSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
mainSearchBar.delegate = self;

UISearchDisplayController *searchCon = [[UISearchDisplayController alloc] initWithSearchBar:mainSearchBar contentsController:self ];
searchCon.delegate = self;
searchCon.searchResultsDataSource = self;
searchCon.searchResultsDelegate = self;

mainTableView.tableHeaderView = mainSearchBar;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM