0

I have a tableview that I create in code. Previously, I assigned it absolute coordinates but as I switch over to autolayout in Storyboard- I don't know how to link it to the element above it.

Basically, the tableView is supposed to be a subview of the container view and it is supposed to be about 12 points below a textField. Can anyone suggest correct way to do this?

This is how I currently create and position the tableview.

_autocompleteTableView = [[UITableView alloc] initWithFrame:
                              CGRectMake(20, 200, 280, 300) style:UITableViewStylePlain];
[self.view addSubview:_autocompleteTableView];

The constraints in storyboard for the textfield above it are:

textfield.leading = superview.leading+20
textfield.trailing+20 =superview.trailing
textfield.top = elementAbove.bottom+12
textfield.height=30;

Thanks for any suggestions.

picciano
  • 22,341
  • 9
  • 69
  • 82
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Possible duplicate of [Swift | Adding constraints programmatically](https://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically) – mfaani Dec 20 '17 at 01:31

2 Answers2

0

Try using anchors. It's super easy to use as simple as constraints in SB.

I think this guide is the best to show how it works. Scroll down to part with title Creating Constraints with Layout Anchors.

Hope it helps!

Tung Fam
  • 7,899
  • 4
  • 56
  • 63
0

Try this

Declare

bool once = YES;

then

-(void)viewDidLayoutSubviews
{

   if(once)
   {
           _autocompleteTableView = [[UITableView alloc] initWithFrame:
                          CGRectMake(20, 200, 280, 300) style:UITableViewStylePlain];

            [self.view addSubview:_autocompleteTableView]; 

            self.autocompleteTableView.translatesAutoresizingMaskIntoConstraints = NO;


           NSLayoutConstraint* con1=[NSLayoutConstraint constraintWithItem:  self.view  attribute:NSLayoutAttributeBottom  relatedBy:NSLayoutRelationEqual    toItem:self.autocompleteTableView attribute:NSLayoutAttributebottom multiplier:1 constant:20];



           NSLayoutConstraint* con2=[NSLayoutConstraint constraintWithItem:   self.view  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual  toItem:self.autocompleteTableView attribute:NSLayoutAttributeLeading multiplier:1 constant:-20];


           NSLayoutConstraint* con3=[NSLayoutConstraint constraintWithItem:  self.view  attribute:NSLayoutAttributeTraling relatedBy:NSLayoutRelationEqual toItem:self.autocompleteTableView attribute:NSLayoutAttributeTraling multiplier:1 constant:20];                  


           NSLayoutConstraint* con4=[NSLayoutConstraint constraintWithItem:   self.autocompleteTableView  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual   toItem:self.textfield attribute:NSLayoutAttributeBottom multiplier:1 constant:12];   


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


           [self.view layoutIfNeeded];

            once = NO;



   }


}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87