在导航标题下添加搜索栏

ios*_*ios 3 objective-c uinavigationcontroller uisearchbar ios

这是我的代码:

[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;
Run Code Online (Sandbox Code Playgroud)

像那个屏幕截图

Sha*_* BS 13

如果你想要完全看起来像你在上一个问题中发布的图像,那么你可以像下面这样做,

[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];
Run Code Online (Sandbox Code Playgroud)

导航栏如下所示,并将搜索栏更改为您的要求

在此输入图像描述

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)
Run Code Online (Sandbox Code Playgroud)

故事板设置:

每一件事情是好的,你刚刚需要设置故事板视图控制器embed in navigation controller,(如果你不没有完成),然后在视图控制器,只需添加一个搜索栏,并outletviewcontroller.swift随后做如下修改

@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()
}
Run Code Online (Sandbox Code Playgroud)