一旦搜索完成,UISearchController的搜索栏就会向下移动

时间:2015-07-03 07:09:50

标签: objective-c uisearchcontroller

我在集合视图的标题部分添加了一个搜索栏和两个按钮,为了实现这一点,我添加了一个视图(我在其上添加了搜索栏)和它下面的两个按钮。如下图所示。

enter image description here

- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    _headerView= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    [_headerView.searchView addSubview:self.searchController.searchBar];
    return _headerView;

}

以下是我在ViewDidLoad方法

中添加的行
  self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = YES;
    self.searchController.searchBar.delegate = self;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar sizeToFit];

enter image description here

每当我使用搜索栏进行搜索,然后在搜索完成后将其关闭,搜索栏会向下移动。

enter image description here

我无法长时间解决这个搜索栏移位问题,任何关于如何避免这种情况的线索,也是在移动SearchBar后不再响应。

2 个答案:

答案 0 :(得分:1)

这是一个布局问题。虽然可以通过在此方法中将搜索栏视图的大小移回原始大小来解决此问题

 - searchBarTextDidEndEditing: 

示例:

在你的.h文件中,

@property (nonatomic) CGRect originalViewSize;

在您的.m文件中,

- (void) viewDidAppear:(BOOL)animated
{
self.originalViewSize = self.searchController.searchBar.frame;
}


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
self.searchController.searchBar.frame = self.originalViewSize ;
}

答案 1 :(得分:0)

我可以通过以下方法解决此问题,方法是将其从superview中删除,然后重新将其添加到视图中。

from abc import abstractmethod, ABCMeta

class AbstractBase(object):
  __metaclass__ = ABCMeta

  @abstractmethod
  def must_implement_this_method(self):
      raise NotImplementedError()

class ConcreteClass(AbstractBase):

   def extra_function(self):
       print('hello')

   # def must_implement_this_method(self):
     # print("Concrete implementation")


d = ConcreteClass()  # no error
d.extra_function()