【发布时间】:2011-04-13 00:52:26
【问题描述】:
在 Xcode 中,如何将UIButton 的背景设置为图像?或者,如何在UIButton 中设置背景渐变?
【问题讨论】:
标签: ios iphone xcode image button
在 Xcode 中,如何将UIButton 的背景设置为图像?或者,如何在UIButton 中设置背景渐变?
【问题讨论】:
标签: ios iphone xcode image button
在tableViewCell 或collectionViewCell 中设置图像时,这对我有用:
将以下代码放入您的cellForRowAtIndexPath 或cellForItemAtIndexPath
// 获取指向单元格的指针。答案假定您已经这样做了,但这里是为了完整性。
CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];
// 从文档库中抓取图片并设置到单元格中。
UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
注意: xCode 似乎对我很感兴趣。我不得不重新启动 xCode 和模拟器,它工作正常。
这假设您已将 cheeseThumbnail 设置为 IBOutlet...以及其他一些东西...希望您对表格/集合视图足够熟悉并且可以适应它。
希望这会有所帮助。
【讨论】:
Swift 3.0中按钮的背景图片代码
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
希望这会对某人有所帮助。
【讨论】:
完整代码:
+ (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];
【讨论】:
我们可以在一行中使用此代码设置图像
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
【讨论】:
斯威夫特
像这样设置按钮图像:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
其中myImageName 是您的资产目录中的图片名称。
【讨论】:
如果 setBackgroundImage 对我没有帮助,但 setImage 对我有帮助
【讨论】:
无需任何代码即可设置背景图片!
只需在 Main.storyboard 中按您想要图像的按钮,然后在右侧的 实用程序 栏中,按 属性检查器 并将背景设置为您想要的图像! 确保您在左侧的支持文件中有您想要的图片。
【讨论】:
这会起作用
UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
【讨论】: