0

我正在尝试将 UISearchBar 放在 .xib 文件的工具栏上。我可以将搜索栏拖放到工具栏上,但它显示以下错误。

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

请指导我如何将 UISearchBar 包含到 xib 的工具栏中。

4

3 回答 3

1

据我所知,除非您使用IPAD进行开发,否则不能UISearchBar直接UIToolBarIPHONE中添加,您需要先将 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];
于 2013-02-25T10:57:14.283 回答
0

以编程方式:

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
}
于 2013-02-25T11:17:32.410 回答
0

既然 Apple 在Xcode 8.2中修复了这个问题,您就可以执行此 Interface Builder 了。我认为他们之前禁用了它,因为在 iOS 8.0 之前的 iOS 上不允许弹出窗口,并且工具栏中的搜索栏意味着大多数时候都会使用弹出窗口。但是他们忘记在 iOS 8.0 中禁用它。

于 2016-12-21T15:52:44.210 回答