圧縮する必要があるUISearchBarがあり(スクリーンショットを参照)、タッチまたはisActiveすると大きなサイズに拡張されます。
どうすればこれを行うことができますか?現在、私の検索バーはIBを介してビューに配置されています。
ありがとう
圧縮する必要があるUISearchBarがあり(スクリーンショットを参照)、タッチまたはisActiveすると大きなサイズに拡張されます。
どうすればこれを行うことができますか?現在、私の検索バーはIBを介してビューに配置されています。
ありがとう
検索バーのこのデリゲートを使用します:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self setTheSearchBarWithRect:newRect];//newRect is CGRect;
}
-(void)setTheSearchBarWithRect:(CGRect)frame{
[UIView animateWithDuration:(1.5f)
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
yourSearchBar.frame = frame;
}
completion:^(BOOL finished){
}];
}
以下のデリゲートでは、元のフレームを使用して上記の関数を呼び出します。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;
通知を表示/非表示にするキーボードにNSNotifcationリスナーを追加し、これに基づいてUISearchBarフレームを調整することをお勧めします。
- (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];
}
そして今、フレームを調整するために2つのカスタム関数が必要です。
- (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,50,100,self.searchBar.frame.size.Height);
}
else {
//resize search bar
self.searchBar.frame = CGRectMake (10,50,200,self.searchBar.frame.size.Height);
}
[UIView commitAnimations];
}
編集の開始と終了時に検索バーのフレーム(場所とサイズ)を変更するだけです。
例:開始時
sbar.frame = CGRectMake(sbar.frame.origin.x - 100., sbar.frame.origin.y, sbar.frame.size.x + 100., sbar.frame.size.y);
編集終了時に、元の場所でコントロールをバックコントロールします。
sbar.frame = CGRectMake(sbar.frame.origin.x + 100., sbar.frame.origin.y, sbar.frame.size.x - 100., sbar.frame.size.y);