用一句话解释我的问题有点困难,所以我把这两张图片放在这里。
我想从我的应用程序中获取此行为。起初我使用ButtonBarItems获得了简单的navigationBar。
点击搜索按钮后我想要这样的行为:
如何实现这种行为? 我起床只有一个想法 - 创建新视图并放置它,但它似乎不是最好的做法。
答案 0 :(得分:4)
如果搜索按钮点击了rite,你想隐藏并显示搜索栏,如下所示我使用标签t访问视图例如我拿了一个搜索栏和一个栏按钮项目并将它们放在左右两侧导航栏如下
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, 250,44)];
searchBar.placeholder = @"Search";
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc]initWithCustomView:searchBar];
searchBarItem.tag = 123;
searchBarItem.customView.hidden = YES;
searchBarItem.customView.alpha = 0.0f;
self.navigationItem.leftBarButtonItem = searchBarItem;
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"Search" style:UIBarButtonItemStylePlain target:self action:@selector(whenSearchClicked:)];
self.navigationItem.rightBarButtonItem = leftItem;
}
- (void)whenSearchClicked:(id)sender
{
NSArray *buttonsArray = self.navigationController.navigationBar.topItem.leftBarButtonItems;
for(UIBarButtonItem *item in buttonsArray)
{
if(item.tag == 123 && item.customView.hidden)
{
item.customView.hidden = NO;
if([item.customView isKindOfClass:[UISearchBar class]])
[item.customView becomeFirstResponder];
UIBarButtonItem *rightItem = self.navigationController.navigationBar.topItem.rightBarButtonItem;//single rite item for this example
[rightItem setTitle:@"Cancel"];
}
else
{
item.customView.hidden = YES;
if([item.customView isKindOfClass:[UISearchBar class]])
[item.customView resignFirstResponder];
UIBarButtonItem *rightItem = self.navigationController.navigationBar.topItem.rightBarButtonItem;//single rite item for this example, if u hav more than one just get the array "self.navigationController.navigationBar.topItem.rightBarButtonItems" and set by using tag like above
[rightItem setTitle:@"Search"];
}
}
}
希望这会有所帮助