iJose iJose - 3 months ago 13
iOS Question

iOS 8 : how to set height programmatically for a UITableview in a freeform ViewController

Rookie in iOS.
I have a Freeform ViewController, which has an UIImageView and
UITableView in it.

The Problem i am facing here is when i try to scroll the tableview
some of the items stay hidden because of the height it has taken in the freeform
viewcontroller.

I tried the following code to set the UItableview's height programmatically
according to the size of the screen.

let screenSize: CGRect = UIScreen.mainScreen().bounds
tableview.frame = CGRectMake(0,imageview.frame.height, 280, (screenSize.height -imageview.frame.height))


The above code failed to resize the tableview.

Is there any other way that i can set the UITableview size at Runtime or
in the storyboard.

Solution

I resolved the issue after laid out my both views using custom height and width after specifying translatesAutoresizingMaskIntoConstraints = true for both the views. I did as per my design and app requirement.

Answer

if you are using auto layout or constraints, you have to

tableview.translatesAutoresizingMaskIntoConstraints = true

before applying the new frame, the new frame is not updating because it's getting over-riden by auto layout and constraints.

EDIT :

1- if you have [adjust scroll view insets] enabled in your view controller in story boards disable it, and test and see.

2- do you create the imageView programatically? or from interface builder, and do you have constraints on it?

3- did you know that the status bar has a height and you have to compensate for it too?

4- do you use a navigation bar?

Comments