【发布时间】:2015-10-12 21:02:36
【问题描述】:
我正在开发一个 iPhone 应用程序,并且我正在继承 UINavigationBar 来创建我的自定义导航栏。我想要的是一个更高的导航栏,以及一个 UISearchBar 在 titleView 中。我想在导航栏中有更多空间,因为我想在底部的搜索栏下方放置一个分段控件。这是我到目前为止所做的:
// Custom navigation bar implementation
const CGFloat BUNavigationBarHeightIncrease = 38.0f;
@implementation BUNavigationBar
// This resizes the navigation bar height
- (CGSize)sizeThatFits:(CGSize)size {
CGSize amendedSize = [super sizeThatFits:size];
amendedSize.height += BUNavigationBarHeightIncrease;
return amendedSize;
}
// This repositions the navigation buttons
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *classNamesToReposition = @[@"UINavigationButton"];
for (UIView *view in [self subviews]) {
if ([classNamesToReposition containsObject:NSStringFromClass([view class])]) {
CGRect frame = [view frame];
frame.origin.y -= BUNavigationBarHeightIncrease;
[view setFrame:frame];
}
}
}
@end
我正在将此导航栏分配给这样的导航控制器:
- (IBAction)searchButton:(id)sender {
SearchViewController *searchViewController = [[SearchViewController alloc] init];
UINavigationController *searchNavigationController = [[UINavigationController alloc] initWithNavigationBarClass:[BUNavigationBar class] toolbarClass:nil];
[searchNavigationController setViewControllers:[NSArray arrayWithObjects:searchViewController, nil]];
[self presentViewController:searchNavigationController animated:YES completion:nil];
}
在 SearchViewController 的 viewDidLoad 中,我有这个:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.searchBar = [[UISearchBar alloc] init];
self.cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButton:)];
[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[self.searchBar setPlaceholder:@"Search"];
// For debugging purposes
[self.searchBar setBackgroundColor:[UIColor greenColor]];
[self.navigationItem setTitleView:self.searchBar];
[self.navigationItem setRightBarButtonItem:self.cancelButton];
}
这就是屏幕的样子: Simulator screenshot
搜索栏位于中间,框架已被拉伸以填满整个导航栏(以绿色显示)。相反,我想要的是让搜索栏保持在默认高度,并向上移动到导航栏的顶部。我怎样才能做到这一点?
【问题讨论】:
标签: ios uinavigationbar uisearchbar