簡體   English   中英

iOS7:無法將搜索欄添加到導航控制器

[英]iOS7: Not able to add search bar to Navigation Controller

在我的SearchViewController.m ,我添加了

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    [self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];


    self.searchResultsView.dataSource = self;
    self.searchResultsView.delegate = self;

    NSLog(@"fetching businesses");
    [self fetchBusinesses];

    // fix UITableViewCell height
    self.tableView.rowHeight = 90;
}

但是我看不到搜索欄。 我所看到的是
在此處輸入圖片說明

我想念什么?

謝謝

您可以通過編程方式添加搜索欄,如下所示:

使UISearchBarUISearchDisplayController的屬性,即

 @property (nonatomic,strong) UISearchBar *searchBar ; @property (nonatomic,strong) UISearchDisplayController *searchDisplayController; 

然后編寫以下代碼:

- (void)viewDidLoad 
{
     searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        searchBar.delegate =self;
        self.searchBar.showsCancelButton =TRUE;
        [self.searchBar setHidden:NO];
        [self.searchBar becomeFirstResponder];
        [self.searchDisplayController setActive:YES];

        [self.view addSubview:self.searchBar];

        self.searchDisplayController =[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
        self.searchDisplayController.delegate =self;
        self.searchDisplayController.searchResultsDataSource= self;
        self.searchDisplayController.searchResultsDelegate = self;

}

您還需要為同一ViewController實現UISearchDisplayDelegate,UISearchBarDelegate委托

希望這段代碼對您有用。 試試看,讓我知道您的反饋。

現在我找到了答案,我只是對它進行了測試,並且最終效果良好,

 // init search bar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    searchBar.showsCancelButton=YES;

    // set up searchDisplayController
    UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
                                                   initWithSearchBar:searchBar contentsController:self];
    searchController.delegate = self;


    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    [self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];

暫無
暫無

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

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