gpt4 book ai didi

iphone - 为 UIButton 设置背景图片

转载 作者:太空狗 更新时间:2023-10-30 03:16:56 29 4
gpt4 key购买 nike

我是 Objective C 的新手。如何将此按钮的背景设置为图像(图像已经在 XCode 的资源文件夹中,“blue_button.png”)而不是 clearColor?此外,我更愿意使用图像作为按钮的形状而不是 UIButtonTypeRoundedRect。

btnClear = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

btnClear.frame = CGRectMake(115, 350, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

btnClear.backgroundColor = [UIColor clearColor];

[btnClear addTarget:self action:@selector(clearAction:)

forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

我知道如何在 Interface Builder 中执行此操作,但我更愿意了解如何在 XCode 中执行此操作。

最佳答案

这个有效:

UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btnClear.frame = CGRectMake(115, 200, 90, 40);
[btnClear setTitle:@"Clear" forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClear];

关于iphone - 为 UIButton 设置背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4608454/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com