2017-10-25 2 views
1

私のアプリケーションのUI内にUISearchControllerを配置しようとしています。レイアウトは次のとおりです。クリックしたときにUISearchBarのフレーム/境界が変更される

  • 黄:ViewControllerを
  • レッド:別のViewController
  • ブラック:YellowViewController

enter image description here

私はのUISearchViewを入れたい内のコンテナ黒いコンテナ内のUISearchController。

私のコードは次のようである:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsViewController]; 
UISearchBar* searchBar = self.searchController.searchBar; 
searchBar.frame =_searchBarContainer.bounds; 
[_searchBarContainer addSubview:searchBar]; 
[_searchBarContainer layoutIfNeeded]; 

それは正しい場所にUISearchBarを配置します

しかし、私は検索フィールドを選択した場合には、コンテナの境界を超えるバーを拡大します:

enter image description here

これを解決し、サイズ/外観の変更を避けるにはどうしたらいいですか?

注:translatesAutoresizingMaskIntoConstraintsオプションとclipToBoundsオプションで再生するオプションを試してみましたが、成功しませんでした。私はiOSのUIの専門家ではないので、私は正確な答えを感謝します。ありがとう

+0

固定幅または固定最大幅を設定しましたか? – SteeBono

+0

searchBar.frameをサブビューに追加する前にsearchBar.frameを設定してください。 – SteeBono

+0

@SteeBonoサンプルコードでは、前にsearchBarフレームを設定しています:( – Addev

答えて

1

私の研究によると、SearchBarを選択するたびに、UISearchControllerが表示されます。このUISearchControllerは、常にsearchBarの幅がUIViewControllerに等しく、これはUISearchControllerを表しています。

私の解決策は、UISearchControllerSearchBarにフレームが間違っている場合に、もう一度SearchBar 'フレームを設定します。以下のコードを試してみてください。

@interface ViewController() <UISearchControllerDelegate> 

@property (nonatomic, strong) UISearchController* searchController; 
@property (weak, nonatomic) IBOutlet UIView *searchBarContainer; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsViewController]; 
    UISearchBar* searchBar = self.searchController.searchBar; 
    self.searchController.delegate = self; 
    searchBar.frame =_searchBarContainer.bounds; 
    [_searchBarContainer addSubview:searchBar]; 
    [_searchBarContainer layoutIfNeeded]; 



} 

- (void)willPresentSearchController:(UISearchController *)searchController { 
    [searchController.searchBar addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; 
} 

- (void)willDismissSearchController:(UISearchController *)searchController{ 
    [searchController.searchBar removeObserver:self forKeyPath:@"frame"]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { 
    if (object == self.searchController.searchBar) { 
    if (!CGSizeEqualToSize(self.searchController.searchBar.frame.size, _searchBarContainer.frame.size)) { 
     self.searchController.searchBar.superview.clipsToBounds = NO; 
     self.searchController.searchBar.frame = CGRectMake(0, 0, _searchBarContainer.frame.size.width, _searchBarContainer.frame.size.height); 
    } 
    } 
} 

@end 

は、少なくとも、それは動作します:)

それとも

  • あなたが作成することができますあなたの場合は、これですべての問題を持っていない場合は、その後SearchBarを含む別のUIViewControllerは_searchBarContainerに追加します。
  • UISearchControllerの代わりにUISearchBarUITableViewを使用してください。それは扱いが簡単です。
1

有用な情報が見つかりました。

UISearchBarをタップすると呼び出されるメソッドがいくつかあります。 このメソッドの一部が呼び出されると、UISearchBarのフレームが値を変更します。

これらの方法の1つは、UIViewControllerに等しい幅を埋めることを試みます。これらの方法のいずれかの内側にあなたのフレーム値を設定する

試してください:あなたは、デフォルト値を上書きこのように

をsearchBarShouldBeginEditing

searchBarTextDidBeginEditing

bye

関連する問題