UISearchBar在自定义NavigationBar中

时间:2015-10-12 21:02:36

标签: ios uinavigationbar uisearchbar

我正在开发一个iPhone应用程序,我正在继承UINavigationBar来创建我的自定义导航栏。我想要的是一个更高的导航栏,以及一个位于titleView中的UISearchBar。我想在导航栏中留出更多空间,因为我想在搜索栏下面的底部放置一个分段控件。以下是我到目前为止所做的事情:

// 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

搜索栏位于中间,框架已拉伸以填充整个导航栏(以绿色显示)。相反,我想要的是搜索栏保持默认高度,并移动到导航栏的顶部。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:5)

只需在viewDidLoad

中调用以下功能即可
 - (void)CustomizeNavigationBarForSearch
{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;

    self.navigationController.navigationBar.translucent = YES;
    self.navigationItem.hidesBackButton = YES;

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"ImgNavBarBG"] forBarMetrics:UIBarMetricsDefault];

    //Menu
    UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnMenu setFrame:CGRectMake(-5, 5, 35, 35)];
    [btnMenu setImage:[UIImage imageNamed:@"BtnMenuDrawer"] forState:UIControlStateNormal];
    [btnMenu setImage:[UIImage imageNamed: isMain ? @"BtnMenuDrawer" : @"BtnBack"] forState:UIControlStateNormal];
    [btnMenu addTarget:self action:isMain ? @selector(clickOnMenu:) : @selector(clickOnBack:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *fixedspace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedspace.width = -12.0f;
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnMenu];
    self.navigationItem.leftBarButtonItems = @[fixedspace, leftBarButton];

    //Search
    searchPlayer = [[UISearchBar alloc] initWithFrame:CGRectMake(30, 2, SCREEN_WIDTH - 80 - 30, 30)];
    searchPlayer.placeholder = @"Search";
    searchPlayer.showsCancelButton = NO;
    searchPlayer.delegate = self;

    searchPlayer.searchBarStyle = UISearchBarStyleMinimal;

    searchPlayer.barTintColor = [UIColor clearColor];
    [searchPlayer setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [searchPlayer sizeToFit];


    //Search Text field customization
    [UITextField appearanceWhenContainedIn:[UISearchBar class], nil].backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    [UITextField appearanceWhenContainedIn:[UISearchBar class], nil].textColor = [UIColor whiteColor];
    [UITextField appearanceWhenContainedIn:[UISearchBar class], nil].font = THEME_FONT_BOLD(15.0);
    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
    [barButtonAppearanceInSearchBar setTitleTextAttributes:@{NSFontAttributeName:THEME_FONT(15.0),
                                                         NSForegroundColorAttributeName : [UIColor whiteColor]
                                                         } forState:UIControlStateNormal];
    [barButtonAppearanceInSearchBar setTitle:@"Cancel"];

    [searchPlayer setValue:[UIColor lightGrayColor] forKeyPath:@"_searchField._placeholderLabel.textColor"];

    self.navigationItem.titleView = searchPlayer;
}