I have one UITableView. I want to set constraints to it programatically. Following are the constraints which i want to set.
- Leading
- Trailing
- Top
- Bottom
It will be really very helpful if somebody provides sample example.
I have one UITableView. I want to set constraints to it programatically. Following are the constraints which i want to set.
It will be really very helpful if somebody provides sample example.
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];
}
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)
}
}
}
Try This,
Now add this attributes also,
than add