[英]How to set these constraints programmatically?
Currently I have my viewcontroller as below 目前我有我的viewcontroller如下
--highestView--
--topView--
--tableView--
I would like to make the topView
dissappear when I scroll down which means tableView
will be exactly below the highestView
. 向下滚动时,我想使
topView
消失,这意味着tableView
将恰好位于highestView
。
So upon scrolling up, I would like them to go back to original view which is like above. 因此,在向上滚动时,我希望它们返回到上面的原始视图。
My code is as below:- 我的代码如下:
-(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;
}
}
Scrolling down works exactly like how I want but scrolling up fails. 向下滚动的方式与我想要的完全相同,但是向上滚动失败。 Currently, the
topView
shows up at the back of tableView
upon scrolling up. 目前,
topView
在后面显示出来tableView
在向上滚动。 How can I fix this ? 我怎样才能解决这个问题 ?
set the height cosntraint for your topView and make it 0 when you want to hide it! 设置topView的高度cosntraint,并在要隐藏它时将其设置为0!
Look into using Masonry for applying constraints. 考虑使用砌体来施加约束。
For adding height constraint: 要添加高度限制:
[toppView mas_makeConstraints:^(MASConstraintMaker *make) {
make. mas_height.equalTo(100); //just make this 0 when you want to hide the topView
}];
You can achieve this using XIB constraints. 您可以使用XIB约束来实现。
HeightView
<-> TopView
<-> TableView
these three views are connected to each other with Vertical Space
and Bottom Space
constraints. HeightView
<-> TopView
<-> TableView
这三个视图通过“ Vertical Space
和“ Bottom Space
约束相互连接。
Then add height constraint to TopView
and make it's IBOutlet
as: 然后将高度限制添加到
TopView
并将其设置为IBOutlet
:
@IBOutlet weak var topViewHeightCon: NSLayoutConstraint!
And then your own code can reused as: 然后您自己的代码可以重用为:
-(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()
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.