Objective c 我能';t以编程方式显示图像
我正试图在我正在使用的IOS(iPad)应用程序上从我的桌面加载并显示png。我需要以编程的方式来做,而不是用故事板。这是我的密码Objective c 我能';t以编程方式显示图像,objective-c,uibutton,uiimage,Objective C,Uibutton,Uiimage,我正试图在我正在使用的IOS(iPad)应用程序上从我的桌面加载并显示png。我需要以编程的方式来做,而不是用故事板。这是我的密码 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; [myButton addTarget:self action:@selector(myCustomFunction:) forControlEvents:UIControlEventTouchUpInside]; [myBu
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:@selector(myCustomFunction:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:@"annie.png"] forState:UIControlStateNormal];
myButton.frame = CGRectMake(14.0, 10.0, 74.0, 46.0);
myButton.frame = CGRectMake(4000, 4000, 4000, 4000);
[self.view addSubview:myButton];
当我运行应用程序时,(使用IOS模拟器),它只会给我一个空白的白色屏幕。我是编程新手,我确信我遗漏了一些非常明显的东西。但我不知道是什么…我没有得到任何错误,但它只是不显示照片。我需要照片以UIbutton的形式,因为它可能需要有触摸选项。
谢谢 这条线路几乎肯定会引起问题。摆脱它
myButton.frame = CGRectMake(4000, 4000, 4000, 4000);
不要立即设置图像,而是尝试设置一些文本以确保按钮出现
[myButton setTitle:@"I am a button" forState:UIControlStateNormal];
最后,尝试设置图像(而不是背景图像)
这个答案是正确的。主要是
setImage:
和setBackgroundImage:
之间的区别。哇!谢谢你,能够做到这一点是我计划的50%。我终于可以前进了。谢谢
[myButton setImage:[UIImage imageNamed:@"annie.png"] forState:UIControlStateNormal];