user6631314
user6631314

Reputation: 1958

IOS/Autolayout/Objective-C: Create Constraint in Code that References Element in Storyboard

I am creating a tableview in code and want to place it using autolayout directly below the lowest most element on the screen in storyboard. The position of the lowermost element varies--thanks to autolayout--based on the contents of elements on the screen.

Is there anyway to grab an element that has an outlet from storyboard and create a constraint to it using code?

This is how I create table:

_myTableView = [[UITableView alloc] initWithFrame:
                              CGRectMake(20, 714, 280, 200) style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.scrollEnabled = NO;
    _myTableView.layer.cornerRadius = 5;
    _myTableView.backgroundColor = [UIColor whiteColor];
    _myTableView.rowHeight=28;
    [self.scrollView addSubview:_myTableView]; 

This is how I normally create constraints in code:

 NSLayoutConstraint *tableToBottomElement = [NSLayoutConstraint 
constraintWithItem:_myTableView attribute:NSLayoutAttributeTop 
relatedBy:NSLayoutRelationEqual toItem:self.bottomElement 
attribute:NSLayoutAttributeBottom multiplier:1 constant:12];

Basically I want the tableview to appear 12 points below the bottom element.

However, after creating the constraint, it shows up as an Unused variable. How would I get the tableview to adjust to this constraint? And is there a way to make the table initially appear in the right position?

Upvotes: 0

Views: 46

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Don't forget to remove the most bottom constraint of the bottom element if it is at the bottom end of the scollview to hook the tableview to it and hook tableview's bottom to the scrollview

   -(void)viewDidLayoutSubviews
   {
      if(once){

      _myTableView.translatesAutoresizingMaskIntoConstraints = NO;

      NSLayoutConstraint *con1 = [NSLayoutConstraint 
      constraintWithItem:_myTableView attribute:NSLayoutAttributeTop 
      relatedBy:NSLayoutRelationEqual toItem:self.bottomElement 
      attribute:NSLayoutAttributeBottom multiplier:1 constant:12];

     NSLayoutConstraint *con2 = [NSLayoutConstraint 
     constraintWithItem:_myTableView attribute:NSLayoutAttributeTrailing 
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView  
     attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];

      NSLayoutConstraint *con3 = [NSLayoutConstraint 
      constraintWithItem:_myTableView attribute:NSLayoutAttributeLeading
      relatedBy:NSLayoutRelationEqual toItem:self.scrollView  
      attribute:NSLayoutAttributeLeading multiplier:1 constant:0];

      NSLayoutConstraint *con4 = [NSLayoutConstraint 
      constraintWithItem:_myTableView attribute:NSLayoutAttributeBottom 
      relatedBy:NSLayoutRelationEqual toItem:self.scrollView  
      attribute:NSLayoutAttributeBottom multiplier:1 constant:0];

      [self.scrollView  addConstraints:@[con1,con2,con3,con4]];

      [self.view layoutIfNeeded];

      once = NO;

      }

   }

Upvotes: 1

Related Questions