【问题标题】:UISearchBar Table Section Index Titles OverlapUISearchBar 表部分索引标题重叠
【发布时间】:2010-01-14 21:08:19
【问题描述】:

当 UISearchBar 位于表头内时,如何更改它的宽度,以使部分索引不与文本字段重叠?我想重新创建您在电话应用程序的联系人视图中找到的相同布局。

【问题讨论】:

    标签: iphone cocoa-touch uitableview uisearchbar


    【解决方案1】:

    一种可能的解决方案是将自定义视图添加到 tableHeaderView。这就是我所做的:

    searchBar 是我在头文件中声明的 UISearchBar。我创建了一个 UIView 并为整个宽度添加了一个 UINavigationBar 并在其顶部添加了短 UISearchBar 。然后,我终于把 UIView 分配给了 tableHeaderView。

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 290.0, 44.0)];
    
    UIView *customTableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    UINavigationBar *dummyNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    [customTableHeaderView addSubview:dummyNavigationBar];
    [customTableHeaderView addSubview:searchBar];
    [dummyNavigationBar release];
    
    self.tableView.tableHeaderView = customTableHeaderView;
    [customTableHeaderView release];
    

    更优雅的解决方案是使用 contentStretch 属性拉伸 UISearchBar。我仍然必须尝试并检查它是否有效。

    【讨论】:

    • @Snej hmm.. 那么这是我知道的唯一解决方案.. 谢谢你让我知道 :)
    • 也适用于故事板。
    • 嗯.. 我不确定为什么答案被否决。在 cmets 中告诉我如何改进答案。
    【解决方案2】:

    您的意思是屏幕右侧的索引字母 A-Z 超出了搜索栏,如此屏幕截图所示,“A”和“B”出现在搜索栏的右边缘?

    (a) 我找不到任何方法来避免这个字母溢出问题。此处由 Scott McCammon 的回答链接的方法被 Apple 禁止(即便如此我也不确定它是否能解决这个问题)。

    (b) 我在 iPod touch(第 4 代)的 iOS 6.0.1 中的 Apple Contacts 应用程序中看到了这种行为。

    (c) 我想从逻辑上讲它是一个功能而不是错误,因为搜索栏位于表格视图的标题中。标题是表格视图的一部分,并且像表格视图的一部分一样滚动,因此可能像在表格视图的其余部分上一样绘制索引字母表是有意义的。

    当您有范围按钮时,这个问题变得更加严重。右侧没有为范围按钮保留空间,因此索引字母直接绘制在最右侧按钮的顶部。

    【讨论】:

      【解决方案3】:

      如果您使用 UISearchDisplayController,所需的行为将自动工作,如 this answer 中所述。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 2014-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多