Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS cannot change frame of UIButton

I am currently stuck with a very weird problem. I am using iOS 6 and Xcode 4.5. I have a storyboard with a UIViewController and a UIButton in it. I made a weak UIButton outlet to my .h file.

In the .m file I want to change the frame of the button but nothing happens. Yet I have an UIImageView created and connected the same way as the UIButton but I can change the UIImageView's frame without any problems.

When I do this:

NSLog(@"%@", button) 

I get this output:

<UIButton: 0xa185a40; frame = (191 330; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0xa185640>>

And these are the relevant parts of my code:

-(void)viewDidLayoutSubviews{
    [self addStampGrid];
}

-(void)addStampGrid {

    // ...

    [_button setFrame:CGRectMake(10,10,10,10)];
}
like image 367
Bins Ich Avatar asked Oct 07 '12 15:10

Bins Ich


3 Answers

You can use this

[_button setTranslatesAutoresizingMaskIntoConstraints:YES];

after this you can set frame of your button.

It may give some warnings in log about constraints, as this property override constraints you gave in autolayout.

like image 130
Achyut Sagar Avatar answered Oct 13 '22 18:10

Achyut Sagar


I had the same weird problem using XCode 4.5 and iOS 6 as deployment target. I am not using story board though. I managed to solve it by disabling the option "Use Autolayout" in the Interface Builder. (In the file inspector->Interface builder document.

Hope this helps

like image 23
streem Avatar answered Oct 20 '22 09:10

streem


You can change frame of your UIButton by changing layer's frame of UIButton

 [self.button.layer setFrame:] 

import QuartzCore framework in your class header before using this. This will work with auto layout also.

like image 11
Neeraj Avatar answered Oct 20 '22 11:10

Neeraj