objective-c - 如何将 UISerachBar 添加到 UIToolBar?

标签 objective-c uisearchbar uitoolbar

我现在尝试这样的事情:

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIViewController *searchBarContainer = [[UIViewController alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchBar];

NSArray* buttons = [NSArray arrayWithObjects:fixed, searchBarContainer, nil];

[detailToolbar setItems:buttons animated:NO];

最佳答案

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem  * searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];

UIToolbar  * searchToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0+20,[self view].bounds.size.width,32)];

[searchToolbar setItems:[NSArray arrayWithObjects:searchBarButtonItem,homeButtonItem, nil] animated:YES];

关于objective-c - 如何将 UISerachBar 添加到 UIToolBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5903394/

相关文章:

iphone - 从 mapView 检索当前用户位置,而 map 不在屏幕上

iphone - iOS Facebook 登录但无法连接?

ios - 如何按降序对日期数组进行排序

objective-c - 子类的 Swift 数组处理

swift - 如何在swift中创建两个搜索栏?

ios - 如何更改 UISearchBar 中 UITextField 的属性

iphone - 当 UISearchDisplayController 处于非事件状态时保持搜索词可见

iphone - 在 Interface Builder 中设置 UIBarButtonItem View 后,如何以编程方式修改它?

ios - 如何获取 UIAlertController 的框架宽度?

swift - 用于键盘输入的简单 UIToolarAccessoryView 的布局约束错误