2

https://stackoverflow.com/a/14235624/855680の回答に従ってアニメーション化します。UISearchBarこれは、より小さな幅で始まり、アクティブなときに iPhone 画面の全幅に拡大します。[キャンセル] ボタンがまったく表示されないことを除いて、期待どおりに展開されます。setShowsCancelButton:animatedとの両方searchBarTextDidBeginEditing:に電話をかけてみましsearchDisplayControllerWillBeginSearch:たが、役に立ちませんでした。私は何が欠けていますか?これが私のコードです:

HomeViewController.h

#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController <UISearchBarDelegate, UISearchDisplayDelegate>
@end

HomeViewController.m

#import "HomeViewController.h"

@interface HomeViewController ()

@property (strong, nonatomic) UISearchDisplayController *sdc;
@property (strong, nonatomic) UISearchBar *searchBar;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add dummy buttons to navigation bar.
    UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:nil];
    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
    [self.navigationItem setLeftBarButtonItems:@[btn1, btn2] animated:YES];

    // Add UISearchBar.
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 0, 150, 44)];
    self.sdc = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.sdc.delegate = self;
    [self.navigationController.navigationBar addSubview:self.searchBar];
}

// From this point onwards, pretty much copy-paste from the StackOverflow answer.
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(adjustFrame:) name:UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(adjustFrame:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)adjustFrame:(NSNotification *) notification {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if ([[notification name] isEqual:UIKeyboardWillHideNotification]) {
        // revert back to the normal state.
        self.searchBar.frame = CGRectMake (100, 0, 150, 44);

    }
    else  {
        //resize search bar
        self.searchBar.frame = CGRectMake (0,0,320,self.searchBar.frame.size.height);
    }

    [UIView commitAnimations];
}

// Try to catch the editing event and display the Cancel button.
// BOTH DON'T WORK.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    [controller.searchBar setShowsCancelButton:YES animated:YES];
}

@end
4

4 に答える 4

0

これがiOS7の最も簡単なソリューションです。

- (void) viewDidLoad
{
     self.navigationItem.leftBarButtonItem = LEFT_ITEM;
     self.navigationItem.rightBarButtonItem = SEARCH_ICON;
}

- (void)searchIconPressed
{
     self.navigationItem.leftBarButtonItem = nil;
     self.navigationItem.rightBarButtonItem = nil;

    self.navigationItem.titleView = self.searchBar;

    UIBarButtonItem* cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelSearch)];
    [self.navigationItem setRightBarButtonItem:cancel animated:YES];

    [self.searchBar becomeFirstResponder];
}

これで、[キャンセル] ボタンのある検索バーも表示されます。

于 2014-02-24T11:27:45.147 に答える
0

ナビゲーション バーに検索バーを配置する理由はありますか? 検索バーをビューのどこかに配置している場合、コードが機能するはずだと思います。何かを配置したい場合、ナビゲーションバーは扱いにくいです。通常、rightbarbuttonitem / leftbarbuttonitem のようなナビゲーションバーのオブジェクトを置き換えるビューまたはアイテムを定義します。題名

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
于 2013-09-29T07:40:33.597 に答える