简体   繁体   English

为UIButton设置背景图像

[英]Setting a background image for UIButton

I am a newbie with Objective C. How would I set the background of this button to an image (image is already in the resources folder in XCode, "blue_button.png") instead of clearColor? 我是Objective C的新手。如何将此按钮的背景设置为图像(图像已经在XCode的资源文件夹中,“blue_button.png”)而不是clearColor? Also, I would prefer that I use the image as the shape of the button rather than the UIButtonTypeRoundedRect. 另外,我更喜欢使用图像作为按钮的形状而不是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];

I know how to do this in Interface Builder but I would rather learn about doing it in XCode. 我知道如何在Interface Builder中执行此操作,但我宁愿学习如何在XCode中执行此操作。

This works: 这有效:

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];

See the UIButton class reference : -setBackgroundImage:forState: 请参阅UIButton类引用-setBackgroundImage:forState:

Create your button with type UIButtonTypeCustom instead of UIButtonTypeRoundedRect if you don't want to use the rounded corner style. 创建类型的按钮UIButtonTypeCustom代替UIButtonTypeRoundedRect如果你不想使用圆角风格。

You need to declare your button with the following UIButtonTypeCustom type: 您需要使用以下UIButtonTypeCustom类型声明您的按钮:

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

Then you should be able to use the follow: 然后你应该能够使用以下内容:

[btnClear setImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];

There are 6 different control states that can be set. 可以设置6种不同的控制状态。

enum {
   UIControlStateNormal               = 0,
   UIControlStateHighlighted          = 1 << 0,
   UIControlStateDisabled             = 1 << 1,
   UIControlStateSelected             = 1 << 2,
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
};

Here are some references that may help: 以下是一些可能有用的参考资料:

UIButton Class Reference UIButton类参考

UIControl Class Reference UIControl类参考

You can use [UIColor colorWithPatternImage:] to set background color to image. 您可以使用[UIColor colorWithPatternImage:]将背景颜色设置为图像。 To use image you should change button style to custom. 要使用图像,您应该将按钮样式更改为自定义。

无需将按钮设置为UIButtonTypeCustom - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state方法也适用于UIButtonTypeRoundedRect

if you want to set button background with images on the Internet, using SDWebImage is an elegant choice: 如果你想在互联网上设置图像的按钮背景,使用SDWebImage是一个优雅的选择:

#import <SDWebImage/UIButton+WebCache.h>

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
[button setBackgroundImageWithURL:imageURL forState:UIControlStateNormal];

Swift 4+ Swift 4+

var btnClear = UIButton()
btnClear = UIButton(type: .custom)
btnClear.frame = CGRect(x: 115, y: 200, width: 90, height: 40)
btnClear.setBackgroundImage(UIImage(named: "blue_button.png"), for: .normal)
view.addSubview(btnClear)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM