在Xcode中,如何将UIButton
的背景设置为图像?或者,如何在UIButton
?
答案 0 :(得分: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];
答案 1 :(得分:17)
这将有效
UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
答案 2 :(得分:5)
如果它可以帮助任何人setBackgroundImage
不能为我工作,但setImage
做了
答案 3 :(得分:3)
您可以设置没有任何代码的背景图片!
只需在 Main.storyboard 中按下您想要图像的按钮,然后在右侧的实用程序栏中按属性检查器并将背景设置为您想要的图像! 确保您在左侧的支持文件中找到了您想要的图片。
答案 4 :(得分:2)
在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 ...以及其他一些内容......希望您对表/集合视图足够熟悉并且可以适应这种情况。
希望这有帮助。
答案 5 :(得分:2)
<强>夫特强>
按如下方式设置按钮图像:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
其中myImageName
是资产目录中图片的名称。
答案 6 :(得分:0)
在一行中,我们可以使用此代码设置图像
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
答案 7 :(得分:0)
Swift 3.0
中按钮背景图片的代码 buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
希望这会对某人有所帮助。