2017-05-31 93 views
0

目前,我有我的ViewController如下如何以编程方式设置这些约束?

--highestView-- 
--topView-- 
--tableView-- 

我想使topView dissappear当我向下滚动,这意味着tableView将是完全highestView下方。

所以滚动后,我想让他们回到原来的视图,就像上面一样。

我的代码如下: -

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

    CGFloat scrollPos = self.tableView.contentOffset.y ; 

    if(scrollPos >= self.currentOffset){ 
     //Fully hide your toolbar 
     [UIView animateWithDuration:2.25 animations:^{ 
      self.topView.hidden = YES; 
      self.topViewTopConstraint.active = NO; 
      self.theNewConstraint2.active = NO; 
      self.theNewConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.highestView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 
      self.theNewConstraint.active = YES; 

     }]; 
    } else { 
     //Slide it up incrementally, etc. 
     self.theNewConstraint.active = NO; 
     self.topView.hidden = NO; 
     self.topViewTopConstraint.active = YES; 
     self.theNewConstraint2 = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 
     self.theNewConstraint2.active = YES; 



     //self.topView.hidden = NO; 
    } 
} 

向下滚动的作品完全一样我多么希望,但向上滚动失败。目前,topView在滚动后显示在tableView的后面。我怎样才能解决这个问题 ?

回答

0

为您的topView设置高度坐标并将其设置为0当您想隐藏它时!

研究使用Masonry来应用约束。

为了增加高度约束:

[toppView mas_makeConstraints:^(MASConstraintMaker *make) { 
    make. mas_height.equalTo(100); //just make this 0 when you want to hide the topView 
}]; 
0

你可以做到这一点使用XIB约束。
HeightView <→TopView <→TableView这三个视图通过Vertical SpaceBottom Space约束互相连接。

然后高度约束添加到TopView并使它的IBOutlet为:

@IBOutlet weak var topViewHeightCon: NSLayoutConstraint! 

然后你自己的代码可以重用为:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGFloat scrollPos = self.tableView.contentOffset.y; 
    if(scrollPos >= self.currentOffset){ 
    topViewHeightCon.constant = 0 
    UIView.animate(withDuration: 0.3, animations: { 
     self.view.layoutIfNeeded() 
    }) 
    } else { 
    topViewHeightCon.constant = originalHeight 
    UIView.animate(withDuration: 0.3, animations: { 
     self.view.layoutIfNeeded() 
    }) 
    } 
} 
相关问题