在UINavigation上添加UISearchBar

在UINavigation上添加UISearchBar的方法

代码如下:

 1 // 创建UIView
 2     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 250, 54)];
 3     [self.view addSubview:view];
 4     // 创建搜索栏
 5     UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 10, 250, 30)];
 6     /**
 7      *  setTranslucent如果设置成no,则状态栏及导航样不为透明的,界面上的组件就是紧挨着导航栏显示了,所以就不需要让第一个组件在y方向偏离44+20的高度了
 8      */
 9     [searchBar setTranslucent:NO];
10     searchBar.placeholder = @"请输入需要查询的商品";
11     [view addSubview:searchBar];
12     // 添加导航栏
13     UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
14     UINavigationItem *item = [[UINavigationItem alloc] init];
15     item.titleView = view;
16     [navigationBar pushNavigationItem:item animated:YES];
17     [self.view addSubview:navigationBar];
原文地址:https://www.cnblogs.com/aggie/p/4744749.html