【问题标题】:adding UISearchBar to UICollectionviewController embedded into NavigationController IOS6将 UISearchBar 添加到嵌入到 NavigationController IOS6 中的 UICollectionviewController
【发布时间】:2012-12-04 15:20:19
【问题描述】:

我想向 UICollectionViewController 添加一个搜索栏,其嵌入方式如下: UItabbarController > UINavigationbarController > UICollectionViewController > SearchBar (!) 在此视图中,搜索栏将取代 NavigationBar。

在相同的设计下,如果我使用 UITableViewController 测试上述内容,搜索栏显示正常(通过编程和故事板)

问题是当我使用 StoryBoard 框架时,我无法在 UICollectionViewController 上添加搜索栏;它只是位于视图的中间,我不知道如何将它移动到顶部。另外,它总是出现在 UICollectionview 下方,所以它是不可见的。

因此,以编程方式采取另一条路线:

-(void)viewWillAppear:(BOOL)animated{
self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.searchBarTop setPlaceholder:@"Enter your command here"];
self.searchDC = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBarTop contentsController:self];
self.searchBarTop.delegate = self;

[[self navigationController] setNavigationBarHidden:NO animated:animated];
[self.navigationController.navigationBar addSubview:self.searchBarTop];
}

这样,搜索栏就可以正常显示了。但不幸的是,当我输入一些文本时,它会在视图上方消失 - 大概是因为底层 navBar 这样做了 - (不知道为什么......)

我不确定为什么搜索栏可以使用 UITableViewController,以及为什么 UICollectionViewController 会如此痛苦。 也就是说,任何人都知道为什么搜索栏/导航栏消失了,我该如何解决这个问题?

欢迎任何解决方案..

谢谢! -A

【问题讨论】:

    标签: ios uinavigationcontroller uisearchbar uicollectionview


    【解决方案1】:

    添加一个标题并将SearchBar 放入其中(这是我过去所做的)。话虽如此,我已经养成了几乎从不使用UITableViewController(除非我正在实施StaticCellTableView)或UICollectionViewController的习惯。我的建议是实施标准UIViewController 并添加您的UICollectionView。将CollectionView 缩小一些并将SearchBar 放在顶部。这使您可以拥有始终显示的SearchBar(我的用户通常更喜欢它而不是滚动到顶部来更改、编辑搜索)

    【讨论】:

    • 谢谢!我同意,这可能是我必须考虑的解决方案,因为 UICollectionviewController 似乎使用了 100% 的视图。我想让搜索栏按需隐藏/可见,所以导航栏提供了一个简单的出路。对 UIViewController 执行相同操作,同时允许 uicollectionview 正确调整大小将需要一些返工。如果可以的话,我宁愿避免它。现在。
    • 在标题中没有,因为这个选项不是我要找的(搜索栏会随着视图滚动)。我可能会尝试另一个 - UIViewController - 虽然我倾向于认为我应该符合 UICollectionViewController/UICollectionView 对。
    • 如果你不想让它随着滚动视图滚动,你需要使用 ViewController 然后添加你的 CollectionView
    • 你是对的,但这会在代码中增加异构性,使其可重用性大大降低。我可能不得不重新考虑整个事情。我仍然不明白为什么它在 UITableviewController 上运行良好,而在 UICollectionViewController 上却如此糟糕。无论如何,杰,谢谢你的帮助!
    • 您不必将其添加到代码的标题中。这可以在 Interface Builder 中完成。
    【解决方案2】:

    我使用以下代码将 UISearchBar 添加到 UICollectionViewController。 不幸的是,我无法让 UISearchDisplayController 工作。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), 44)];
        self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        self.searchBar.delegate = self;
        [self.collectionView addSubview:self.searchBar];
        [self.collectionView setContentOffset:CGPointMake(0, 44)];
    }
    
    - (void) viewWillAppear:(BOOL)animated{
        // to show search bar
        [self.collectionView setContentOffset:CGPointMake(0, 0)];
        // to hide search bar
        [self.collectionView setContentOffset:CGPointMake(0, 44)];
    }
    
    -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
        [searchBar setShowsCancelButton:YES animated:YES];
    }
    
    -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        [searchBar setText:@""];
        [searchBar setShowsCancelButton:NO animated:YES];
        [searchBar resignFirstResponder];
    }
    

    【讨论】:

    • 嗨尤金,谢谢!事实上,这几乎是 Jay 的解决方案,而这并不是我所需要的。在此期间,我找到了这个:stackoverflow.com/questions/10617330/…,它可以很好地满足我的需求。干杯。
    • 除此方法外,您可以将布局的 headerReferenceSize 属性设置为 CGSizeMake(320.0,44.0) 以便内容将在点 44.0 之后开始
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2020-11-14
    • 2016-02-26
    • 2014-05-16
    相关资源
    最近更新 更多