我正在将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;
无论我设置搜索栏的宽度,高度和位置,都不会改变搜索栏的高度/宽度。它的宽度和高度始终相同。
有人可以建议如何改变吗?
答案 0 :(得分: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;
答案 1 :(得分:0)
目前尚不清楚您希望搜索栏的大小。如果您将其设为导航项的titleView,它将调整导航栏上的任何按钮,并调整旋转。这就是你想要的吗?
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *sb = [[UISearchBar alloc] init];
self.navigationItem.titleView = sb;
}