以编程方式在iPhone上设置按钮背景图片

26
在Xcode中,如何将UIButton的背景设置为图像?或者,如何在UIButton中设置背景渐变?
8个回答

66

完整代码:

+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    // in case the parent view draws with a custom color or gradient, use a transparent color
    button.backgroundColor = [UIColor clearColor];

    return button;
}

UIImage *buttonBackground = UIImage imageNamed:@"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:@"blueButton.png";

CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);

UIButton *button = [FinishedStatsView buttonWithTitle:title
                                               target:target
                                             selector:action
                                                frame:frame
                                                image:buttonBackground
                                         imagePressed:buttonBackgroundPressed
                                        darkTextColor:YES];

[self addSubview:button]; 

设置图片:

UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];

要删除一张图片:

[button setBackgroundImage:nil forState:UIControlStateNormal];

在将按钮的背景设置为图像之前,为什么要创建图像的副本?如果不这样做,我们是否会冒着内存泄漏或异常行为的风险,还是只是为了允许图像拉伸以填充其按钮? - Kevin

17

这样会起作用

UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];

5

如果有帮助的话,setBackgroundImage 对我没用,但是 setImage 有用。


使用setImage可以设置前景图像...如果您想设置背景图像,请使用setBackgroundImage:<#(UIImage *)#> forState:<#(UIControlState)#>。 - redestructa

3

您可以不用任何代码设置背景图片!

只需在Main.storyboard中按下要添加图像的按钮,然后在右侧的属性检查器中按下属性并将背景设置为所需的图像!请确保您想要的图片位于左侧的支持文件中。


2

Swift

按照以下方式设置按钮图像:

let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)

其中myImageName是您资产目录中图像的名称。


2

当在tableViewCellcollectionViewCell中设置图像时,以下代码对我有效:

将以下代码放置在您的cellForRowAtIndexPathcellForItemAtIndexPath中:

//获取指向单元格的指针。 假设您已经完成了这一步骤,但在此处为完整起见。

CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];

// 从文档库中获取图像并将其设置到单元格中。
UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];

注意: 我在这个问题上遇到了xCode卡住的情况。我不得不重新启动xCode和模拟器,才让它正常工作。

这里假设你已经设置了cheeseThumbnail作为IBOutlet...还有其他一些东西...希望你对表格/集合视图足够熟悉,可以将其整合进去。

希望这能帮到你。


0
我们可以使用这行代码来设置图像。
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];

0

Swift 3.0中设置按钮背景图的代码:

buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)

希望能对大家有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接