在導航欄上添加搜索框的問題

自定義一個搜索框添加到導航欄上,

 _searchBar = [[ZJBSearchBar alloc]initWithFrame:CGRectMake(7,6, screenSize.width-14,32) withString:@"請輸入關鍵詞"];

 _searchBar.delegate = self;

 UIView * viewd=self.navigationController.navigationBar;

 [viewd addSubview:_searchBar];

遇到一個問題,導航進入下一個界面的時候,搜索框還在導航欄的位置,用

[self.navigationController.view addSubview:_searchBar];

還是有這個問題,最後發現這樣解決

創建一個view,把導航欄添加到view上,把view設爲導航的titleView

 _searchBar = [[ZJBSearchBar alloc]initWithFrame:CGRectMake(7,6, screenSize.width-14,32) withString:@"請輸入關鍵詞"];

 _searchBar.delegate = self;  

 //將搜索條放在一個UIView

 UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenSize.width, 44)];

 searchView.backgroundColor = [UIColor clearColor];

 [searchView addSubview:_searchBar];

 self.navigationItem.titleView = searchView;




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章