2016-10-12 7 views
1

の下に検索バーを追加することは私のコードです:がここにナビゲーションタイトル

ここ
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, 500, 800)]; 
self.navigationItem.title = @"locations"; 
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.view.frame.size.width,110.0)]; 
UISearchBar *searchBar1 = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 40, 240, 34)]; 
[self.navigationController.view addSubview:searchBar1]; 
elf.navigationItem.title = @"locations"; 
//self.navigationItem.titleView = searchController.searchBar; 
self.navigationController.navigationBar.barTintColor = `Color grayColor]; 
self.definesPresentationContext = YES; 

like that screen shot

+3

可能な複製を変更する[下の検索バーを追加する方法ナビゲーションタイトル](http://stackoverflow.com/questions/39994506/how-to-add-search-bar-under-the-navigation-title) –

+1

なぜ同じ質問が2回目? –

+0

私は自分の出力にしか届かなかった。誰か助けてください。 – ios

答えて

10

をgo..Tryあなたは前の質問に投稿されているので、あなたは以下のようにすることができます。

[self.navigationController.navigationBar setTranslucent:NO]; 
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack]; 
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]]; 
[self.navigationController.navigationBar setShadowImage:[UIImage new]]; 

UISearchBar *searchBar = [[UISearchBar alloc] init]; 
searchBar.placeholder = @"search"; 
self.title = @"Locations"; 
searchBar.frame = CGRectMake(0, 0, self.navigationController.view.bounds.size.width, 64); 
searchBar.barStyle = UIBarStyleDefault; 
[searchBar setTranslucent:NO]; 
searchBar.barTintColor = [UIColor redColor]; 
searchBar.backgroundImage = [UIImage new]; 
[self.view addSubview:searchBar]; 

とナビゲーションバーの下のように見えますし、あなたの条件に

enter image description here

を検索バーを変更SWIFTコード

navigationController?.navigationBar.isTranslucent = false 
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    navigationController?.navigationBar.barStyle = .black 
    navigationController?.navigationBar.barTintColor = UIColor.red 
    navigationController?.navigationBar.shadowImage = UIImage() 
    title = "Location"; 


    let searchBar = UISearchBar() 
    searchBar.placeholder = "Search" 
    searchBar.frame = CGRect(x: 0, y: 0, width: (navigationController?.view.bounds.size.width)!, height: 64) 
    searchBar.barStyle = .default 
    searchBar.isTranslucent = false 
    searchBar.barTintColor = UIColor.red 
    searchBar.backgroundImage = UIImage() 
    view.addSubview(searchBar) 

ストーリーボードのセットアップ:

すべてのもの大丈夫です、あなたはストーリーボードビューコントローラをセットアップする必要がありますO embed in navigation controller、(uはまだ行われていない場合)、ビューコントローラでは、単に検索バーを追加し、次の操作を行いviewcontroller.swiftためoutletを作るには、の

@IBOutlet weak var searchBar: UISearchBar! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    //navigationController?.navigationBar.isTranslucent = false //set it in strory board 
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    //navigationController?.navigationBar.barStyle = .black ////set it in strory board 
    //navigationController?.navigationBar.barTintColor = UIColor.red ////set it in strory board 
    navigationController?.navigationBar.shadowImage = UIImage() 
    title = "Location"; 

    searchBar.barStyle = .default 
    searchBar.isTranslucent = false 
    searchBar.barTintColor = UIColor.red 
    searchBar.backgroundImage = UIImage() 
} 
+0

それは完璧に動作しますが、私はすぐにそのコードが必要です。 – ios

+0

これを追加してストーリーボードの色を変更することは可能ですか? – ios

+0

あなたのコードはありがとうございました。 – ios

1

uは完全にあなたのイメージのようになりたい場合は、このコード

self.navigationItem.title = @"locations"; 
UISearchBar *searchBar1 = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 34)]; 
[self.view addSubview:searchBar1]; 
NSLayoutConstraint *contTop = [NSLayoutConstraint constraintWithItem:searchBar1 
           attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom 
           multiplier:1.0 constant:0]; 

NSLayoutConstraint *consHeight = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual toItem:nil 
           attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 
           constant:34]; 

NSLayoutConstraint *contLeading = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeLeading 
            relatedBy:NSLayoutRelationEqual toItem:self.view 
            attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; 
NSLayoutConstraint *conttrailing = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeTrailing 
            relatedBy:NSLayoutRelationEqual toItem:self.view 
            attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; 
[NSLayoutConstraint activateConstraints:@[contTop, consHeight,contLeading,conttrailing]]; 
[searchBar1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view layoutSubviews]; 
self.navigationItem.title = @"locations"; 
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; 
searchBar1.barTintColor= [UIColor orangeColor]; 
self.definesPresentationContext = YES; 
+0

ナビゲーションボーダーの色を変更する方法 – ios

+0

[self.navigationController.navigationBar.layer setBorderWidth:2.0]; //その動作を確認するだけです。 [self.navigationController.navigationBar.layer setBorderColor:[[UIColor whiteColor] CGColor]];検索バーの境界線の色も同様に設定できます。 – Darshana