如何在滚动UITableView时创建NSLayoutConstraint

时间:2016-08-01 12:11:39

标签: ios objective-c uitableview uiscrollview nslayoutconstraint

我在SingleView上有UITableView。表视图的约束为左0,右0,下0,上520.我需要在向上滚动时将顶部520更改为0,并在滚动底部时返回520.如何使其成为可能。 如何更改NSLayoutConstraint滚动 -

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

我试图实现如此但失败的

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.tag == 1){

        if (scrollView.contentOffset.y < pointNow.y) {

            self.heightLogTabTableView.constant = 0;

            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];

        }else if (scrollView.contentOffset.y > pointNow.y) {

            self.heightLogTabTableView.constant = 520;
            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];
        }
    }
}

5 个答案:

答案 0 :(得分:2)

将代码中的[self.view layoutIfNeeded];替换为 - [_tableView beginUpdates];[_tableView endUpdates];。同时修改变量pointNow如下。

#import "ViewController.h"

@interface ViewController (UIScrollViewDelegate){

}


@end

@implementation ViewController
CGPoint pointNow;

- (void)viewDidLoad{

     [super viewDidLoad];   
     pointNow = CGPointMake(0, 0);

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{



        if (scrollView.contentOffset.y < pointNow.y) {

            self.heightLogTabTableView.constant = 0;

            [UIView animateWithDuration:0.5f animations:^{
                [_tableView beginUpdates];
                [_tableView endUpdates];
            }];

        }else if (scrollView.contentOffset.y > pointNow.y) {

            self.heightLogTabTableView.constant = 520;
            [UIView animateWithDuration:0.5f animations:^{
                [_tableView beginUpdates];
                [_tableView endUpdates];
            }];
        }
    pointNow = scrollView.contentOffset;

}

@end

答案 1 :(得分:1)

更改约束常量后立即尝试setNeedsUpdateConstraints方法。像:

[self.tableView setNeedsUpdateConstraints];

更新 -

如果我理解正确的话,我认为这就是你想要的:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

    if(self.heightLogTabTableView.contentOffset.y<0){
        //it means table view is pulled down like refresh
    }
    else if(self.heightLogTabTableView.contentOffset.y >= 0) {
        //it means table view is being scrolled up
    }    
}

希望这会对你有所帮助。

答案 2 :(得分:0)

这一行代码将解决您对top,bottom,left, right的所有Zero约束的问题

[self.tableView setContentInset:UIEdgeInsetsMake(520, 0, 0, 0)];

答案 3 :(得分:0)

请尝试这个,它会帮助你......

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (scrollView.tag != 1)
    {
        return;
    }

    if(self.heightLogTabTableView.contentOffset.y<0)
    {
        self.heightLogTabTableView.constant = 0;

            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];
    }
    else 
    {
        self.heightLogTabTableView.constant = 520;
            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];
    }    
}

答案 4 :(得分:0)

我在.h文件"@property (nonatomic) CGFloat lastScrollOffset;"

中添加全局变量CGFloat
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.tag == 1){


        if (scrollView.contentOffset.y > _lastScrollOffset) {

            self.heightLogTabTableView.constant = 0;

            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];
        } else if (scrollView.contentOffset.y < _lastScrollOffset) {

            self.heightLogTabTableView.constant = self.informationTask.frame.size.height;

            [UIView animateWithDuration:0.5f animations:^{
                [self.view layoutIfNeeded];
            }];
        }

        _lastScrollOffset = scrollView.contentOffset.y;
    }

    else if (scrollView.tag == 2){

     if (scrollView.contentOffset.y > _lastScrollOffset) {

         self.heightCommentTabTableView.constant = 0;
         [UIView animateWithDuration:0.5f animations:^{
             [self.view layoutIfNeeded];

             self.typeCommentView.hidden = NO;

         }];

     } else if (scrollView.contentOffset.y < _lastScrollOffset) {

         self.heightCommentTabTableView.constant = self.informationTask.frame.size.height + 220;

         [UIView animateWithDuration:0.5f animations:^{
             [self.view layoutIfNeeded];
             self.typeCommentView.hidden = YES;
         }];
        }
    }
}