Here is a strange thing, but I couldn't figure it out. I can not programmatically set frame for the button.
I have a button with IBOutlet as beyondViewButton and I control-dragged it from view to the ViewController.m file @interface and @end section, then @synthesized it.
in the viewDidLoad method, I added this:
//x = x + widthToUse * 0.25;
//y += h + 50;
x = 50; //either hard code x value or set it mathematically, not working
y = 300;
w = 200;
h = 60;
//[beyondViewButton setFrame:CGRectMake(x, y, w, h)]; //not working
beyondViewButton.frame = CGRectMake(x, y, w, h); //not working
//[beyondViewButton setBackgroundImage:[UIImage imageNamed:@"bgImage.png"] forState:UIControlStateNormal]; //this line actually works
//[beyondViewButton setTitle:@"iCool!" forState:UIControlStateNormal];
[beyondViewButton setTitle:@"iShit!" forState:UIControlStateNormal]; //change from iCool to iShit, I can see change
//[self.view addSubview:beyondViewButton]; //either adding this line or not, both work
Why I can not set up the frame?
One thing, I don't know if relevant, is that this button was set up in the "selection view" which comes from Navigation controller and goes to next view/scene. When this button is clicked, app moves from the "selection view" to the next view --- this was implemented via storyboard. I have not mastered storyboard well yet and have lots of uncertainty about it.
Can anyone shed some lights on this? Thanks!!
More diagnosis info: I checked the button.description before and after I set frame code:
2012[25830:12503] beyond button <UIRoundedRectButton: 0x8821cd0; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x8821da0>>
that's before and following is after. Interestingly, the frame values are not what I set, as in code shown here, I have x=50, y=300, w=200, h=60, while NSLog description gives out x=190, y=345, w=110, h=94, which is around the position I saw in run time. It's the values I set in storyboard.
2012[25830:12503] beyond button <UIRoundedRectButton: 0x8821cd0; frame = (190 345; 110 94); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x8821da0>>
If I commented out set frame lines of the code, when I check button description, before and after the button settings, I got 0,0,0,0 as button frame --- although I see buttons in the bottom half of the view. In another word, the line of setting frame does work SOME WAY, but not the way I wanted. It just takes storyboard values, but not those I set by x,y,w,h.
If you have Auto-layout enabled on interface builder, you can also remove the button from view, size it, and then re-add it. Kind of ghetto, but it works.
Something similar to this...
[self.myButton removeFromSuperview];
[self.myButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.myButton sizeToFit];
[self.myView addSubview:self.myButton];