【问题标题】:Table View header hiding like search bar表视图标题像搜索栏一样隐藏
【发布时间】:2011-03-10 15:45:06
【问题描述】:

即使表格的整体内容大小不大于界限,我如何让表格视图的标题视图滚动进出界限。作为 Mail.app 中的一个示例,即使在没有消息的视图中,您仍然可以将搜索栏滚动进出视图(我应该添加没有滚动条)。如何在不使用搜索显示控制器/搜索栏的情况下获得此功能。

【问题讨论】:

    标签: ios objective-c uitableview uiview uikit


    【解决方案1】:

    尝试设置UIScrollView的属性:

    @property(nonatomic) CGPoint contentOffset
    @property(nonatomic) BOOL alwaysBounceVertical
    

    contentOffset 设置为searchBarheight,将alwaysBounceVertical 设置为YES

    这应该可以解决问题!

    【讨论】:

    • 这不起作用,因为偏移量仅允许您在 contentSize 大于边界时向上滚动视图。所以在一个空的表格视图中,你不能再向上滚动它了。不知何故,在 IB 中添加搜索栏会覆盖这一点,这可以在 mail.app 中的任何空邮箱中看到。
    • 我已将答案编辑为包含 alwaysBounceVertical。使用这两个属性可以使它像您在我当前正在处理的项目中描述的那样工作。告诉我!
    【解决方案2】:

    tableHeaderView 设置为UISearchBar 的实例时,您看到的是UITableView 的特殊行为。我相信这种行为是硬编码的。因此,为了您自己的观点,您需要扩展UISearchBar。或者更好的是,创建一次这样的隐藏标题并在需要隐藏行为时将标题包含在其中:

    @interface HidingTableViewHeader : UISearchBar
    
    - (id)initWithHeader:(UIView *)header;
    
    @end
    
    @implementation HidingTableViewHeader
    
    - (id)initWithHeader:(UIView *)header
    {
        // We don't really care about the search bar initialization
        self = [super init];
        if (self) {
            // Remove all the sub-views that make up the search bar UI
            [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            // Override UISearchBar's fixed size
            self.size = header.size;
            // Match the tableHeaderView behavior: auto-resize horizontally
            header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            // Make the header the only sub-view
            [self addSubview:header];
        }
        return self;
    }
    
    @end
    

    像这样使用它:

    self.tableView.tableHeaderView = [[HidingTableViewHeader alloc] initWithHeader:someRegularView];
    

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 2014-08-23
      • 2011-03-08
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2015-11-07
      相关资源
      最近更新 更多