【问题标题】:Hide UISearchBar until user scrolls?隐藏 UISearchBar 直到用户滚动?
【发布时间】:2017-09-12 04:05:07
【问题描述】:

我在表格视图的顶部实现了搜索栏和搜索显示控制器。

当视图加载时,搜索栏和相关范围始终可见。

有没有一种简单的方法来隐藏它,直到用户向下滚动,就像在音乐应用中发生的那样?

【问题讨论】:

    标签: ios ios6 uisearchbar


    【解决方案1】:

    你需要添加搜索栏作为表格视图的标题,然后将viewDidLoad中表格视图的contentoffset属性设置为,

    [self.tableView setContentOffset:CGPointMake(0,44) animated:YES];//or (0, 88) depends on the height of it
    

    对于搜索显示控制器,你也可以试试这个,

    [self.searchDisplayController setActive:NO animated:YES];
    

    【讨论】:

    • 当表格中的所有行都适合屏幕时,该解决方案不会隐藏搜索栏。此类场景的任何已知解决方法?
    • @Zorayr:你有没有为表格找到一个可行的解决方案,其中单元格适合整个屏幕?遇到同样的问题。
    【解决方案2】:

    另一种没有硬编码的方法

    [self.tableView setContentOffset:CGPointMake(0.0, self.tableView.tableHeaderView.frame.size.height) animated:YES];
    

    【讨论】:

      【解决方案3】:

      拉了几个小时的头发后,效果很好

      • 在 iOS 9 上
      • 不依赖于表中的行数
      • 可变高度行

        考虑到搜索栏是tableView.tableHeaderView

        @interface MyTableViewController ()
        
        @property (nonatomic, assign) BOOL firstLayout;
        
        @end
        
        - (void) viewDidLoad {
        [super viewDidLoad];
        
        
        self.firstLayout = YES;
        //your code
        
        }
        
        -(void)viewDidLayoutSubviews{
        
        [super viewDidLayoutSubviews];
        
        if(self.firstLayout){
        CGPoint offset = CGPointMake(0, self.tableView.tableHeaderView.frame.size.height - self.tableView.contentInset.top);
        [self.tableView setContentOffset:offset];
        
            self.firstLayout = NO;
        
        }
        
        }
        

      【讨论】:

      • 全面性大+1。
      【解决方案4】:

      适用于 iOS 7 并使用 UINavigationController

      [self.tableView setContentOffset:CGPointMake(0, self.searchBar.height + self.navigationController.navigationBar.height)];
      

      【讨论】:

        【解决方案5】:

        我在

        中重新加载 tableview 的最佳方式
        - (void)viewWillAppear:(BOOL)animated {
            [super viewWillAppear:YES];     
            [self performSelector:@selector(reload:) withObject:nil afterDelay:0.0];
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
        
        - (void)reload:(__unused id)sender {
                [self.searchDisplayController setActive:YES];
                [self.tableView reloadData];
                [self.refreshControl endRefreshing];
            } else {
                [self.refreshControl endRefreshing];
                [[[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
            }
        }
        

        谢谢

        【讨论】:

          【解决方案6】:

          这在 iOS9+ 上运行良好

          - (void)viewDidLoad {
              [super viewDidLoad];
          
              dispatch_async(dispatch_get_main_queue(), ^{
                  self.tableView.contentOffset = CGPointMake(0, -20);
              });
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-02-14
            • 2013-10-26
            • 2018-10-02
            • 1970-01-01
            • 1970-01-01
            • 2019-08-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多