0

我在SingleView上有UITableView。表視圖有約束左0,右0,底部0,頂部520.我需要更改頂部520到0滾動向上,當滾動底部返回520.如何可以做到這一點。 如何從滾動的改變NSLayoutConstraint -如何在滾動時創建NSLayoutConstraint UITableView

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

創建TopSpace約束和變革的出口它的常量值在scrollView委託,如你所願。 –

+0

相反,跟蹤滾動與indexPath你'willDisplayCell'或'CellForRowAtIndexPath'代表和執行基於行或部分 – NSNoob

+1

@NSNoob UITableView的適當行動繼承的UIScrollView – iSashok

回答

0

這種單一的代碼行會解決你的問題,與所有top,bottom,left, right約束Zero

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

我添加此代碼(void)scrollViewDidScroll:(UIScrollView *)scrollView但它不工作 –

+0

添加它在-viewdidload(),並且不需要實現-scrollViewDidScroll :() – Rajesh

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 
    }  
} 

希望這有助於你。

+0

我看到的邏輯,我需要協調全局變量默認值,並協調滾動,該方法應該比較當前與以前的座標與滾動,並確定如果你向上滾動以改變某些東西如果向下返回座標爲默認值,則約束爲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 
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]; 
      }]; 
    }  
} 
0

我加入全球varible CGFloat的在我.h文件"@property (nonatomic) CGFloat lastScrollOffset;"

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

我需要比較我的全局變量scroll.contentOffset.y。 –

+0

我做得對嗎? –