在UITableView中创建隐藏的UISegmentedControl [英] Creating a hidden UISegmentedControl in a UITableView

查看:68
本文介绍了在UITableView中创建隐藏的UISegmentedControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像iBooks应用一样,当您下拉表格视图时,会出现一个搜索栏和分段控件,可让您在两种类型的视图之间进行搜索和切换.

当您向下拖动足够远时,它会停留在该位置;或者,当您充分向上拖动桌面时,它会隐藏起来.

我正在尝试使用UISegmentedControl来实现相同的功能. 到目前为止,我已经成功地将分段控件添加为表的子视图. (它的Y框架为负,因此请使其停留在表格视图上方.

我还实现了以下代码:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    float yOffset = scrollView.contentOffset.y;

    if (yOffset < -70) {
        [scrollView setContentOffset:CGPointMake(0.0f, -70.0f) animated:YES];
    } else if (yOffset > -10) {
        [scrollView setContentOffset:CGPointMake(0.0f, -11.0f) animated:YES];
    }
}

这很好用,直到我尝试使用分段控件为止.该表的作用就像滚动一样,完全忽略了分段控件(即,如果我点击一个段,它甚至都不会被选中,而是该表向上滚动,隐藏了分段控件.

我确实使用了scrollViewDidScroll方法,但这使它变得有问题并且滚动跳动.

我也尝试制作分段控件的exclusiveTouch = YES,但这没有任何作用.

感谢所有帮助!预先感谢!

解决方案

这是我的有效代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Discover top
        //
        CGFloat topY = scrollView.contentOffset.y + scrollView.contentInset.top;

        if (topY <= self.tableHeaderHeightConstraint.constant) {
            [self setIsScrolledToTop:YES];
        } else {
            [self setIsScrolledToTop:NO];
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Toggle favourite category
        //
        if ([self isScrolledToTop]) {
            //
            //  Show
            //
        } else {
            //
            //  Hide
            //
        }
    }
}

编辑了上面的代码,使其更加通用,但从语法上来说,它是正确的

Like the iBooks app, when you pull down the tableview, a search bar and segmented control appear, to allow you to search and switch between two types of views.

It sticks in that position when you pull down far enough, and alternatively, gets hidden when you pull the tableview up enough.

I am trying to implement the same thing with a UISegmentedControl. So far I have added a segmented control successfully as a subview to the table. (It has a negative Y frame so make it stick above the tableview).

I have also implemented this code:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    float yOffset = scrollView.contentOffset.y;

    if (yOffset < -70) {
        [scrollView setContentOffset:CGPointMake(0.0f, -70.0f) animated:YES];
    } else if (yOffset > -10) {
        [scrollView setContentOffset:CGPointMake(0.0f, -11.0f) animated:YES];
    }
}

This works great, until I try using the segmented control. Where the table will just act like it is scrolling, ignoring the segmented control altogether (i.e. if I tap on a segment, it doesn't even get selected, instead the table scrolls up, hiding the segmented control.

I did use the scrollViewDidScroll method but this made it buggy and the scrolling jumpy.

I also tried to make the segmented control's exclusiveTouch = YES, but this had no effect whatsoever.

I would be thankful for all help! thanks in advance!

解决方案

Here is my code which works:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Discover top
        //
        CGFloat topY = scrollView.contentOffset.y + scrollView.contentInset.top;

        if (topY <= self.tableHeaderHeightConstraint.constant) {
            [self setIsScrolledToTop:YES];
        } else {
            [self setIsScrolledToTop:NO];
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Toggle favourite category
        //
        if ([self isScrolledToTop]) {
            //
            //  Show
            //
        } else {
            //
            //  Hide
            //
        }
    }
}

Edited the above code to make it a bit more generic, but syntactically its correct

这篇关于在UITableView中创建隐藏的UISegmentedControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆