MyJBMe
MyJBMe

Reputation: 2236

Setting the frame of an UIView does not work

I have a problem with the frame-property in iOS 7. I wanna resize some UIViews in the viewDidLoad-method of my UIViewController, but if I do it like int screenHeight = [[UIScreen mainScreen] bounds].size.height; [self.leftSideTableView setFrame: CGRectMake(0, 0, 320, screenHeight)]; the height is set as I want it till the end of the method, but in every other method it is as is has been before!

What's wrong with it or is it just a bug of the compiler or anything else?

Upvotes: 23

Views: 32287

Answers (4)

MyJBMe
MyJBMe

Reputation: 2236

One has to put view resizing into -viewDidLayoutSubviews:! (documentation)

Placing view frame changes into -viewWillAppear: or -viewDidLoad: will not work, because the views are not laying out yet!

Upvotes: 74

lancejabr
lancejabr

Reputation: 118

Check to make sure that auto layout isn't activated in your storyboard file.

To turn it off, look at the inspector in interface builder. Click the icon that looks like a page all the way on the left. In the section "Interface Builder Document" uncheck "Use Auto Layout."

I find it's best to an entire view controller in IB with auto layout, or completely in code. Mixing the two can lead to weird behavior that is hard to debug.

Upvotes: 8

liuyaodong
liuyaodong

Reputation: 2567

  • Check if you are using autolayout in your xib file. If you don't want to use autolayout, uncheck it in your xib file.

  • Change your self.leftSideTableView frame in -viewWillAppear:.

Upvotes: 11

Mick MacCallum
Mick MacCallum

Reputation: 130183

There are several reasons why this might be happening. First of all, you need to make sure that your tableview isn't nil. If you're creating it programmatically, you need to be sure that you're calling alloc/init somewhere before you attempt to set the frame. If self.leftSideTableView is an IBOutlet, this can be caused by forgetting to actually link the outlet to the interface object.

Then, second and less likely, you are creating the table view programmatically and initializing it properly, but you forgot to add it as a subview of one of your on screen views.

Upvotes: 1

Related Questions