I have one UITableView. I want to set constraints to it programatically. Following are the constraints which i want to set.

  1. Leading
  2. Trailing
  3. Top
  4. Bottom

It will be really very helpful if somebody provides sample example.

3 Answers

0
Yash Joshi On

Try This,

NSLayoutConstraint *constraintSet = [NSLayoutConstraint constraintWithItem:YourTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f];
[self.view addConstraint:constraint];

Now add this attributes also,

NSLayoutAttributeTop
NSLayoutAttributeTrailing
NSLayoutAttributeBottom

than add

[yourview addConstraint: constraintSet];
0
Andr3a88 On

In my code usually use the method like below, you can customize to your needs.

-(void)addConstraintForView:(UITableView*)tableView toView:(UIView*)parentView
{

NSMutableArray *constraints = [NSMutableArray new];
[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView
                                                    attribute:NSLayoutAttributeTop
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:parentView
                                                    attribute:NSLayoutAttributeTop
                                                   multiplier:1.0f
                                                     constant:0.0f]];

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView
                                                    attribute:NSLayoutAttributeLeading
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:parentView
                                                    attribute:NSLayoutAttributeLeading
                                                   multiplier:1.0f
                                                     constant:0.0f]];

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView
                                                    attribute:NSLayoutAttributeTrailing
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:parentView
                                                    attribute:NSLayoutAttributeTrailing
                                                   multiplier:1.0f
                                                     constant:0.0f]];

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView
                                                    attribute:NSLayoutAttributeBottom
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:parentView
                                                    attribute:NSLayoutAttributeBottom
                                                   multiplier:1.0f
                                                     constant:0.0f]];

[parentView addConstraints:constraints];
}
0
Srinivasan Raman On

You can the SnapKit pod library to add constrains programmatically. Here get snapkit

import SnapKit

class YourViewController: UIViewController {

var tableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addSubview(tableView)
    tableView.snp.makeConstraints { (make) -> Void in
       make.leading.equalTo(0)
       make.trailing.equalTo(self.view)
       make.top.equalTo(0)
       make.bottom.equalTo(0)
    }
}
}