我需要横向模式下的searchBar帮助。我将它添加到navigationBar titleView - 居中:
self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
self.navigationItem.titleView = self.searchBarTop;
在横向模式下,要适当地适应:
有什么建议吗?
答案 0 :(得分:0)
使用以下代码查找iphone的Orientation,然后相应地设置SearchBar的帧。在ViewDidLoad中设置SearchBar(纵向模式)的帧。
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
}
else
{
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 480.0, 44.0)];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 470.0, 44.0)];
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
}
}
一定会对你有所帮助。
答案 1 :(得分:0)
显然这是定制UISearchBar的灰色区域。我设法使用另一种方式在navigationBar之外移动searchBar。