这是一件奇怪的事情,但我无法弄清楚。我无法以编程方式为按钮设置框架。
我有一个带有 IBOutlet 的按钮作为 BeyondViewButton,我将它从视图控制拖到 ViewController.m 文件的@interface 和@end 部分,然后@synthesized 它。
在 viewDidLoad 方法中,我添加了这个:
//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
为什么我不能设置框架?
我不知道是否相关的一件事是,这个按钮是在来自导航控制器的“选择视图”中设置的,并转到下一个视图/场景。单击此按钮时,应用程序从“选择视图”移动到下一个视图 --- 这是通过情节提要实现的。我还没有很好地掌握故事板,并且对此有很多不确定性。
任何人都可以对此有所了解吗?谢谢!!
更多诊断信息:我在设置框架代码之前和之后检查了 button.description:
2012[25830:12503] beyond button <UIRoundedRectButton: 0x8821cd0; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x8821da0>>
那是之前,之后是之后。有趣的是,帧值不是我设置的,如这里显示的代码,我有 x=50, y=300, w=200, h=60,而 NSLog 描述给出 x=190, y=345, w= 110,h=94,也就是我在运行时看到的位置。这是我在故事板中设置的值。
2012[25830:12503] beyond button <UIRoundedRectButton: 0x8821cd0; frame = (190 345; 110 94); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x8821da0>>
如果我注释掉代码的设置框架行,当我检查按钮描述时,在按钮设置之前和之后,我得到 0,0,0,0 作为按钮框架 --- 尽管我在视图的下半部分看到按钮. 换句话说,设置框架的行确实以某种方式起作用,但不是我想要的方式。它只需要故事板值,而不是我通过 x,y,w,h 设置的值。