如何将UISerachBar添加到UIToolBar?

时间:2011-05-05 19:58:58

标签: 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];

1 个答案:

答案 0 :(得分:8)

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