这是一个奇怪的事情,但我无法弄清楚.我无法以编程方式为按钮设置框架.
我有一个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
Run Code Online (Sandbox Code Playgroud)
为什么我不能设置框架?
有一件事,我不知道是否相关,是这个按钮是在"选择视图"中设置的,它来自导航控制器并进入下一个视图/场景.单击此按钮时,app会从"选择视图"移动到下一个视图---这是通过故事板实现的.我还没有掌握故事板,并且有很多不确定性.
任何人都可以对此有所了解吗?谢谢!!
更多诊断信息:我在设置帧代码之前和之后检查了button.description:
2012[25830:12503] beyond button <UIRoundedRectButton: 0x8821cd0; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x8821da0>>
Run Code Online (Sandbox Code Playgroud)
之前和之后的是之后.有趣的是,帧值不是我设置的,如此处所示的代码,我有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>>
Run Code Online (Sandbox Code Playgroud)
如果我注释掉代码的设置框线,当我检查按钮描述时,按钮设置之前和之后,我得到0,0,0,0作为按钮框架---虽然我看到视图下半部分的按钮.换句话说,设置框架的行确实有效,但不是我想要的方式.它只需要故事板值,但不是我设置的x,y,w,h.
bro*_*oox 13
如果在界面构建器上启用了自动布局,则还可以从视图中删除该按钮,调整其大小,然后重新添加该按钮.一种贫民窟,但它的工作原理.
与此类似的东西......
[self.myButton removeFromSuperview];
[self.myButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.myButton sizeToFit];
[self.myView addSubview:self.myButton];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8440 次 |
| 最近记录: |