user3894033
user3894033

Reputation: 77

How to resize Subview with Autolayout

I have a Tableview view, by some resson, i have to add this tableview in a Scrollview. I used autolayout but this tableview not auto resize when i rotate the screen to landscape.

How to make this tableview including the same size scrollview when i rotate the sreen to landscape?

Here is my code:

scrollViewMain = UIScrollView()
scrollViewMain.contentInset = UIEdgeInsets(top: 34, left: 0, bottom: 52, right: 0)
scrollViewMain.scrollIndicatorInsets = UIEdgeInsets(top: 34, left: 0, bottom: 52, right: 0)
scrollViewMain.delegate = self
scrollViewMain.scrollEnabled = true
scrollViewMain.showsHorizontalScrollIndicator = false
scrollViewMain.showsVerticalScrollIndicator = true
scrollViewMain.indicatorStyle = UIScrollViewIndicatorStyle.White
scrollViewMain.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(scrollViewMain)

view.backgroundColor = BACKGROUND_COLOR


headerView = UIView(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 3))
headerView.backgroundColor = UIColor.clearColor()
headerView.setTranslatesAutoresizingMaskIntoConstraints(false)
headerView.layer.masksToBounds = true

tableViewMain = UITableView()
tableViewMain.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableViewMain.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableViewMain.delegate = self
tableViewMain.dataSource = self
tableViewMain.backgroundColor = UIColor.clearColor()
tableViewMain.backgroundView = nil
tableViewMain.opaque = false
tableViewMain.separatorColor = UIColor(rgb: 0x3e3e3e)
tableViewMain.indicatorStyle = UIScrollViewIndicatorStyle.White
tableViewMain.scrollEnabled = false
tableViewMain.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollViewMain.addSubview(tableViewMain)
tableViewMain.registerClass(MoviesTableViewCell.self, forCellReuseIdentifier: "MoviesTableViewCell")
tableViewMain.tableHeaderView = headerView

let viewDictionary = ["scrollViewMain": scrollViewMain, "tableViewMain": tableViewMain, "headerView": headerView]

let scrollViewMainConstraint_Horizontal:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(
    "H:|-0-[scrollViewMain]-0-|",
    options: nil,
    metrics: nil,
    views: viewDictionary
)
let scrollViewMainConstraint_Vertical:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:|-0-[scrollViewMain]-0-|",
    options: nil,
    metrics: nil,
    views: viewDictionary
)

view.addConstraints(scrollViewMainConstraint_Horizontal as! [AnyObject])
view.addConstraints(scrollViewMainConstraint_Vertical as! [AnyObject])

Upvotes: 0

Views: 76

Answers (1)

ezig
ezig

Reputation: 1229

I think the issue is that the table view doesn't have any constraints, so it doesn't know what rules to follow when the screen rotates. I don't know exactly what constraints you want, but assuming that you want the table view to fill up the whole scroll view, you could add constraints very similar to the ones you used for the scroll view (pin leading, trailing, top and bottom space to superview).

Upvotes: 1

Related Questions