在导航标题下添加搜索栏

3

这是我的代码:

[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
可能是如何在导航标题下添加搜索栏的重复问题。 - Shankar BS
1
为什么同样的问题要问第二次? - Divyesh Savaliya
我无法得到我的输出结果,所以请有人帮帮我。 - ios
你想使用 UISearchBar 还是 UISearchController? - user3182143
我需要 UISearchBar。 - ios
2个回答

14

如果您想完美地像您在先前的问题中发布的图片一样展现自己,那么您可以按照以下方式操作:

[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];

导航栏如下所示,将搜索栏更改为您所需的内容。

图片描述

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)

故事板设置:

一切都很好,您只需要将故事板视图控制器设置为嵌入导航控制器(如果尚未完成),然后在视图控制器中添加搜索栏并为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()
}

在Storyboard中添加它并更改颜色是否可行? - ios

1

这是代码,请试一下:

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;

如何更改导航边框的颜色 - ios
[self.navigationController.navigationBar.layer setBorderWidth:2.0];// 只是为了确保它能正常工作 [self.navigationController.navigationBar.layer setBorderColor:[[UIColor whiteColor] CGColor]]; 您可以以同样的方式设置搜索栏的边框颜色。 - PlusInfosys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接