【问题标题】:UISearchBar scrolls while the UITableView is scrollingUITableView 滚动时 UISearchBar 滚动
【发布时间】:2013-02-05 02:03:59
【问题描述】:

如何实现UISearchBar类似Instagram.app的滚动效果? UISearchBar 向上滚动,UITableView 向上滚动。 UISearchBar 在 origin.y 等于 0 时停止向下滚动;

【问题讨论】:

    标签: iphone ios uitableview scroll uisearchbar


    【解决方案1】:

    我的解决方法如下:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    self.myTableview.delegate = self;
    self.myTableview.dataSource = self;
    self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, searchBarHeight)];
    [self.myTableView setContentInset:UIEdgeInsetsMake(searchBarHeight, 0, 0, 0)];
    [self.myTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    float move = offset.y + searchBarHeight;
    if (move > -2*searchBarHeight && move <3*searchBarHeight) {
        [UIView animateWithDuration:0.1
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction
                         animations:^{
                             CGRect searchBarFrame = self.mySearchBar.frame;
                             searchBarFrame.origin.y = MIN(MAX(-move, -searchBarHeight), 0);
    
                             [self.mySearchBar setFrame:searchBarFrame];
    
                         }
    
                         completion:^(BOOL f) {
    
                         }];
    }
    }
    

    【讨论】:

    • 这对我来说效果不佳。就个人而言,我决定使用 Interface Builder 和 nib 文件在自己的框架中的一个视图中创建带有 UISearchBar、UISearchController 和 UITableView 的 UIViewController。而且效果很好。
    • @wzbozon 有同样的效果吗?为什么不发布您的解决方案的答案:)
    • 很难发帖。一切都是在 Interface Builder 中制作的。
    【解决方案2】:

    创建一个 UISearchBar 实例并将其设置为 tableView Header View

            [tableView setTableHeaderView:yourSearchBar];
    

    它现在将使用 tableView 滚动

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-14
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      相关资源
      最近更新 更多