user2718067
user2718067

Reputation: 1603

iOS, keep tableview's width in ratio with devices' width through auto layout

I had an app contains a tableview that takes place part of the screen's area.

How to make the tableview's width always keeps 1/3 of the screen size whatsoever on iPad2 and iPad Air? Besides, how to make it through auto layout feature in iOS?

Upvotes: 0

Views: 294

Answers (2)

riadhluke
riadhluke

Reputation: 890

create an equal widths constraint with the tableView and its parent view (or the viewController's view, depends on what you want). Edit the the constraint and change the multiplier to either 0.3333 or 3 depending on which view is the constraint's first item, though, i think the super view is always the first item, in that case use 3 as the multiplier.

or programmatically through,

[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:tableView attribute:NSLayoutAttributeWidth multiplier:3 constant:0];

and by the way here's the image of the attributes inspector (where you can change the multiplier) when a constraint is selected on IB,

enter image description here

Upvotes: 1

zooster
zooster

Reputation: 352

let width = self.view.frame.width / 3 self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[yourItem(==width)]|", options: nil, metrics: nil, views: ["yourItem":yourItem]))

Upvotes: 1

Related Questions