0

我正在将 UISearchBar 添加到 UINavigation Bar 中(这是一个基于故事板的应用程序):

//Setup the search bar.
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 44.0f)];
    self.tabBarController.navigationItem.titleView = searchBar;
    searchBar.delegate = self;
    controller = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
    controller.searchResultsDataSource = self;
    controller.searchResultsDelegate = self;

无论我将搜索栏的宽度和高度以及位置设置为什么,它都不会改变搜索栏的高度/宽度。它始终是相同的宽度和高度。

谁能建议如何改变?

4

2 回答 2

0

我曾经遇到过同样的问题,我所做的是将 searchBar 添加到容器视图中(我在显示取消按钮时遇到问题)并将容器视图添加为左栏项

 - (UIBarButtonItem *)createSearchBarHolder {
       UIView *searchBarContainer = [[UIView alloc] initWithFrame:navigationSearchBar.frame];
       searchBarContainer.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
      [searchBarContainer addSubview:navigationSearchBar];
      navigationSearchBar.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
      UIBarButtonItem *searchBarHolder = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
      return searchBarHolder;
  }

在 viewDidLoad 中:

     NSArray *leftBarButtons = [[NSArray alloc] initWithObjects:[self createSearchBarHolder], nil];
     self.navigationItem.leftBarButtonItems = leftBarButtons;
于 2013-11-13T15:50:35.767 回答
0

目前尚不清楚您希望搜索栏的大小。如果将其设为导航项的 titleView,它将针对导航栏上的任何按钮进行调整,并根据旋转进行调整。那是你要的吗?

- (void)viewDidLoad {
    [super viewDidLoad];
    UISearchBar *sb = [[UISearchBar alloc] init];
    self.navigationItem.titleView = sb;
}
于 2013-11-13T16:30:27.090 回答