您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> IOS >> 悬停在tableview上的搜索栏,cell太少的情况下会发生滑动冲突

悬停在tableview上的搜索栏,cell太少的情况下会发生滑动冲突(1/2)

来源:网络整理     时间:2017/6/7 1:55:31     关键词:

关于网友提出的“ 悬停在tableview上的搜索栏,cell太少的情况下会发生滑动冲突”问题疑问,本网通过在网上对“ 悬停在tableview上的搜索栏,cell太少的情况下会发生滑动冲突”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 悬停在tableview上的搜索栏,cell太少的情况下会发生滑动冲突
描述:

有这样一个需求如图所示:

绿色是navigation,高64,红色是一个搜索栏,高44,黄色是一个tableview
约束是红色top距离self.view的top 64 约束名称为:

self.searchBarTopConstraint

由于使用PureLayout,距离(64)可以用下面名称:

self.searchBarTopConstraint.constant

取到

黄色table的top距离红色bottom 0

现在要求:
1.黄色table上滑时红色随黄色上移直到完全被绿色遮盖。
2.黄色table下滑时红色随黄色下移直到回到状态1的位置。
注:
黄色下滑后在任何时候上滑皆会触发要求1
黄色上滑后在任何时候下滑皆会触发要求2

关键代码如下:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat newPoint = scrollView.contentOffset.y;
    CGFloat changePoint = self.oldPoint - newPoint;
    if (changePoint < 0) {
        NSLog(@"上滑");
        if ((self.stopScroll && self.scrollUp) || !self.stopScroll) {
            self.scrollUp = YES;
            if (self.searchBarTopConstraint.constant > 20) {
                //self.searchBarTopConstraint.constant > 20 意思是当搜索框没有完全被上面高64的navigation盖住时(searchBar高44,所以64-44=20)
                self.searchBarTopConstraint.constant += changePoint;
            } else if (self.searchBarTopConstraint.constant <= 20) {
                self.searchBarTopConstraint.constant = 20;
            }
        }
    } else if (changePoint > 0){
        NSLog(@"下滑");
        if ((self.stopScroll && !self.scrollUp) || !self.stopScroll) {
            self.scrollUp = NO;
            if (self.searchBarTopConstraint.constant<64) {
                //self.searchBarTopConstraint.constant < 64 意思是当搜索框没有完全脱离navigation时
                self.searchBarTopConstraint.constant += changePoint;
            } else if (self.searchBarTopConstraint.constant >= 64) {
                self.searchBarTopConstraint.constant = 64;
            }
        }

    }
    self.oldPoint = newPoint;

}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    self.stopScroll = YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.stopScroll = NO;
    [self.searchController.searchBar endEditing:YES];
}

问题:在cell数量足够的情况下比较正常(但不太平滑),不过在cell数量不满一个屏幕时会造成无法滑动的情况,究其原因是在此情况下向上滑动后不知为何会立刻向下滑动,不知怎么解决,求赐教。


解决方案1:

做成组头就自带悬浮效果

解决方案2:

viewDidLoad里设置:

self.automaticallyAdjustsScrollViewInsets = NO;
[self.tableView setContentInset:UIEdgeInsetsMake(44, 0, 0, 0)]; // 使top view 不遮住table view

因为设置了content inset 的原因, 一开始的content offset y 是-44。

相关图片

相关文章