【问题标题】:Why UISearchBar and its scope buttons are shown in one line?为什么 UISearchBar 及其范围按钮显示在一行中?
【发布时间】:2010-08-10 08:59:55
【问题描述】:

我看过 Apple 的示例“TableSearch”,当触摸它的范围按钮时,它会出现在搜索栏下方。 http://developer.apple.com/iphone/library/samplecode/TableSearch/Introduction/Intro.html

但是当我自己制作时,起初它看起来不错,但当我触摸它时,它看起来很丑,范围按钮和搜索栏显示在同一行,如下所示: http://cl.ly/BN9

我需要做什么才能使它像 iPad 中的“TableSearch”示例?

我在 IB 中做所有事情,并尝试从控制器以编程方式修改搜索栏:

    - (void)viewDidLoad {

        [super viewDidLoad];
        self.tableView.rowHeight = 88.0f;
        self.tableView.contentOffset = CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height);
self.searchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight;


    //BELOW DID NOT WORK:
    CGRect b = self.searchDisplayController.searchBar.bounds;
    self.searchDisplayController.searchBar.bounds = CGRectMake(b.origin.x, b.origin.y, b.size.width, self.tableView.rowHeight);
    b = self.searchDisplayController.searchBar.frame;
    self.searchDisplayController.searchBar.frame = CGRectMake(b.origin.x, b.origin.y, b.size.width, self.tableView.rowHeight);

    //BELOW WORKS PERFECT BUT IS A PRIVATE METHOD, HENCE I AM NOT SUPPOSED TO USE IT
    //[self.searchDisplayController.searchBar setCombinesLandscapeBars:NO];

     }

提前致谢。

【问题讨论】:

    标签: ipad uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我也遇到了这个错误,我都向 Apple 提交了报告并请求技术帮助。我会让你知道情况如何。同时,我将简要介绍一下有关此错误的背景信息。

    在 iPhone 上,为了在横向模式下保留宝贵的垂直屏幕空间,UISearchDisplayController 将 UISearchBar 设置为将其搜索栏和搜索字段组合在一个水平布局中。由于增加了屏幕的水平尺寸(横向 480 点),这非常有效。不幸的是,它在 iPad 上运行得不是很好,在横向模式下,UI 的改变一开始就没有必要,因为你有很多垂直空间。在 UISplitViewController 的主视图中,您仍然只有 320 像素的水平显示空间,而不是 iPhone 增加的 480 像素。结果是 iSore。

    可能问题在于 UISearchDisplayController 在其 willRotateToInterfaceOrientation:duration‎: 方法中表现不佳。具体来说,在其 UISearchBar 上设置 combineLandscapeBars 属性之前,无需检查它是否在 iPhone 上。代码中的私有 API 组件有效,因为它修复了 UISearchDisplayController 中的疏忽。但当然,Apple 会因为使用未记录的 API 而让古人对你大发雷霆,所以你不能。在这个问题上,我们真的受制于苹果。

    如果您愿意放弃 UISearchDisplayController 的美观和便利,您可以使用没有 UISearchDisplayController 的 UISearchBar 并自己管理演示的各个方面。显然这需要更多的代码,如果 Apple 的 API 工程师完成他们的工作,这将毫无意义,但它至少可以解决显示错误。

    如果您是 Apple,则可以使用自己的未记录 API,这就是 Mail.app 没有此问题的原因。

    更新

    我向 Apple 提交的错误报告是 #8344719。

    【讨论】:

    • 我发现三个人说他们报告了这个错误。你能说出错误编号吗?
    • Mail.app之所以没有这个问题是因为它没有使用UISearchDisplayController。 Apple 的 iPad 应用程序都没有,这可能就是存在此错误的原因。
    • 我也提交了一个错误。它是#9689005。
    【解决方案2】:

    使用以下代码,您不会收到警告:

    if ([self.searchDisplayController.searchBar respondsToSelector:@selector(setCombinesLandscapeBars:)])
    {
        objc_msgSend(self.searchDisplayController.searchBar, @selector(setCombinesLandscapeBars:), NO );
    }
    

    【讨论】:

      【解决方案3】:

      当你为 searchBar 设置边界和框架时,就像这里的框架:

      blabla.searchBar.frame = CGRectMake(b.origin.x, 
                                          b.origin.y, 
                                          b.size.width, 
                                          self.tableView.rowHeight);
      

      好像身高有问题。范围按钮在搜索栏下方需要一些空间,因此您应该增加边界和框架的高度。

      如果您在某些事件上显示和隐藏范围按钮,则每次都需要调整帧大小。

      【讨论】:

        【解决方案4】:

        我在 iPad 上遇到了这个问题,但如果我将它插入到我的实现文件中,我可以让它工作:

        @synthesize tableView;
        

        我猜想加载 XIB 有问题,但我不知道为什么会修复它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-16
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-04
          • 1970-01-01
          相关资源
          最近更新 更多