【问题标题】:Force scope bar below UISearchBar在 UISearchBar 下方强制范围栏
【发布时间】:2010-04-08 20:04:16
【问题描述】:

我有一个 UISearchBar 和 UISearchDisplayController,一切都很好,但我的范围选择器显示在文本字段旁边而不是它下方。我知道这是设备处于横向时的预期操作,但由于我在 UISplitViewController 的主视图中有 UISearchBar,它最终看起来像这样:

有没有办法强制范围栏在所有界面方向的文本字段下方显示(我知道这在 iPad 上的 Mail.app 中运行良好,所以它可能,但谁知道 Apple 是否决定隐藏选择这样做)

【问题讨论】:

  • 我找到了一种方法,但它不是公共 API。 [self.searchDisplayController.searchBar setCombinesLandscapeBars:NO];确保在 searchDisplayController 的 searchBar 而不是 UISearchBar 本身上调用它。显然搜索显示控制器正在管理这个属性。
  • 我遇到了同样的问题,setCombinesLandscapeBars:NO 确实解决了这个问题,但苹果的示例“TableSearch”在不使用该方法的情况下也得到了相同的结果。您找到其他解决方案了吗?
  • 如果您没有被拒绝,您可以使用此代码,不会引发任何警告并且安全,以防苹果的工程师认为私有方法将不再可用。 if ([self.searchDisplayController.searchBar respondsToSelector:@selector(setCombinesLandscapeBars:)]) { objc_msgSend(self.searchDisplayController.searchBar, @selector(setCombinesLandscapeBars:), NO );}
  • 有人用这种方法逃脱了吗?

标签: iphone cocoa-touch scope uisearchbar uisearchdisplaycontroller


【解决方案1】:

这不是公共 API 的一部分。希望将来会添加。它可以使用私有 API 来完成,查看 UIKit 类转储将对您有所帮助。

祝你好运!

编辑:请注意,正如大多数人已经知道的那样,使用私有 API 可能会导致您被应用商店拒之门外。

【讨论】:

    【解决方案2】:

    我使用 UISearchDisplayDelegate 来显示和隐藏范围栏。

    -(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        [controller.searchBar setShowsScopeBar:YES];
    }
    
    -(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
    {
        [controller.searchBar setShowsScopeBar:NO];
    }
    

    【讨论】:

      【解决方案3】:

      不要使用私有 API。相反,请遵循 Apple 在 IOS 用户界面指南中的建议:

      当存在搜索栏时,其附近会出现一个范围栏。这 范围栏显示在搜索栏下方,无论方向如何, 除非您在代码中使用搜索显示控制器(更多 有关其工作方式的信息,请参阅 UISearchDisplayController 类 参考)。当您使用搜索显示控制器时,范围栏是 时显示在搜索栏右侧的搜索栏中 设备处于横向(纵向,它是 在搜索栏下方)。

      这对我使用故事板很有用。

      【讨论】:

        【解决方案4】:

        为什么不直接以编程方式设置搜索栏?这是我在当前项目中使用的。它会隐藏搜索栏,直到用户向上滚动,并且当他们开始输入项目时,范围栏会加载到文本字段下方。从那里像往常一样过滤:

        - (void)setupSearchBar {
        self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        self.searchBar.showsScopeBar = YES;
        self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Groups", nil];
        self.tableView.tableHeaderView = self.searchBar;
        
        CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
        self.tableView.contentOffset = offset;
        
        self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
                                                                  contentsController:self];
        self.searchController.searchResultsDataSource = self;
        self.searchController.searchResultsDelegate = self;
        self.searchController.delegate = self;
        }
        

        在 viewDidLoad 中致电 [self setupSearchBar];,您将参加比赛。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-31
          相关资源
          最近更新 更多