在导航标题下添加搜索栏

时间:2016-10-12 12:36:15

标签: ios objective-c uinavigationcontroller uisearchbar

这是我的代码:

[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

2 个答案:

答案 0 :(得分:12)

如果您希望看起来像您在上一个问题中发布的图像,那么您可以像下面这样做,

[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

快速代码

    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)

故事板设置:

一切都很好,你只需要将故事板视图控制器设置为embed in navigation controller,(如果你还没有完成),然后在视图控制器中,只需添加一个搜索栏并制作outlet viewcontroller.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()
}

答案 1 :(得分: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;