Andrei  Trotsko Andrei Trotsko - 4 months ago 16
iOS Question

How create NSLayoutConstraint when scrolling UITableView

I have

UITableView
on SingleView. Table view have constraint left 0, right 0, bottom 0, top 520. I need change top 520 to the 0 when scrolling up, and when scrolling bottom return 520. How can make it.
How change
NSLayoutConstraint
from scroll in -

- (void)scrollViewDidScroll:(UIScrollView *)scrollView


I was trying to implement the so-but failed

- (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];
}];
}
}
}

Answer

Replace [self.view layoutIfNeeded]; in your code by-- [_tableView beginUpdates]; and [_tableView endUpdates];. Also modify your variable pointNow as follows.

#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
Comments