在iOS的工具栏中包含UISearchBar

问题描述:

我想将UISearchBar放在.xib文件的工具栏上。 我可以将搜索栏拖放到工具栏上,但会显示以下错误。在iOS的工具栏中包含UISearchBar

ControllerName.xib:error: illegal Configuration: UISearchBar embedded in UIBarButtonItems (Only available ub iPad documents).

请指导我如何包括的UISearchBar到工具栏中的厦门国际银行。

+0

你会在这里得到一个可能的解决方案:http://stackoverflow.com/questions/12309441/ios-using-uisearchdisplaycontroller-with-iseisebar-that-is-uibarbuttonitem-i – 2013-02-25 11:03:57

+0

无需在工具栏中添加搜索栏。 ...你可以添加它并改变它的属性。 – 2013-02-25 11:21:03

据我知道,除非你正在使用IPAD你的发展,你不能直接在UIToolBarIPHONE添加UISearchBar,您需要将的UISearchBar第一添加到customView,然后将其添加到工具栏编程

// your searchbar 
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xposition, yposition, width, height)]; 

//create a customview and make its frame equal to your searchbar 
UIView *searchBarView = [[UIView alloc] initWithFrame:searchBar.frame]; 

// add your searchbar to the custom view 
[searchBarView addSubview:searchBar]; 

//finally add it to your bar item of the toolbar 

UIBarButtonItem *searchBarItem = 
    [[UIBarButtonItem alloc] initWithCustomView:searchBarView]; 
+0

非常感谢它的工作:) – 2013-02-26 04:38:57

+1

这种方法会不会让你的应用程序被拒绝? – 2014-11-24 20:41:58

通过Programetically:

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    [Toolbar sizeToFit]; 

    UISearchBar *testbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,2,250,38)]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(ButtonMethod)]; 
    [barItems addObject:btnCancel]; 

    [Toolbar setItems:barItems animated:YES]; 

    [Toolbar addSubview:testbar]; 

    [self.view addSubview:Toolbar]; 

这里是按键法;

-(void)ButtonMethod 
{ 
    // Write Code for ButtonMethod Method 
} 

现在可以说苹果在的Xcode 8.2固定这个做这个界面生成器。我认为他们以前禁用了它,因为在iOS 8.0之前iOS上不允许弹出窗口,并且工具栏中的搜索栏意味着最多使用弹出窗口。但他们忘了在iOS 8.0中禁用它。