简体   繁体   中英

How to set these constraints programmatically?

Currently I have my viewcontroller as below

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

I would like to make the topView dissappear when I scroll down which means tableView will be exactly below the 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. How can I fix this ?

set the height cosntraint for your topView and make it 0 when you want to hide it!

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.
HeightView <-> TopView <-> TableView these three views are connected to each other with Vertical Space and Bottom Space constraints.

Then add height constraint to TopView and make it's IBOutlet as:

@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()
    })
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM