ios 以编程方式设置按钮背景图像 iPhone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3723831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a button background image iPhone programmatically
提问by Linda
In Xcode, how do you set the background of a UIButton
as an image? Or, how can you set a background gradient in the UIButton
?
在 Xcode 中,如何将 a 的背景设置UIButton
为图像?或者,如何在UIButton
.
回答by Dr. Rajesh Rolen
Complete code:
完整代码:
+ (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];
To set an image:
要设置图像:
UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
To remove an image:
要删除图像:
[button setBackgroundImage:nil forState:UIControlStateNormal];
回答by Pathetic Learner
This will work
这将工作
UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
回答by RanLearns
In case it helps anyone setBackgroundImage
didn't work for me, but setImage
did
万一它帮助任何人setBackgroundImage
不为我工作,但setImage
做了
回答by JELLYFUN
You can set an background image without any code!
无需任何代码即可设置背景图片!
Just press the button you want an image to in Main.storyboard, then, in the utilitiesbar to the right, press the attributes inspectorand set the background to the image you want! Make sure you have the picture you want in the supporting filesto the left.
只需在Main.storyboard 中按下您想要图像的按钮,然后在右侧的实用工具栏中,按下属性检查器并将背景设置为您想要的图像!确保左侧的支持文件中有您想要的图片。
回答by kmiklas
When setting an image in a tableViewCell
or collectionViewCell
, this worked for me:
在 atableViewCell
或 中设置图像时collectionViewCell
,这对我有用:
Place the following code in your cellForRowAtIndexPath
or cellForItemAtIndexPath
将以下代码放在您的cellForRowAtIndexPath
或cellForItemAtIndexPath
// Obtain pointer to cell. Answer assumes that you've done this, but here for completeness.
// 获取单元格指针。Answer 假定您已完成此操作,但此处是为了完整性。
CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];
// Grab the image from document library and set it to the cell.
// 从文档库中获取图像并将其设置为单元格。
UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
NOTE:xCode seemed to get hung up on this for me. I had to restart both xCode and the Simulator, it worked properly.
注意:xCode 似乎对我很感兴趣。我不得不重新启动 xCode 和模拟器,它工作正常。
This assumes that you've got cheeseThumbnail set up as an IBOutlet... and some other stuff... hopefully you're familiar enough with table/collection views and can fit this in.
这假设您已经将 cheeseThumbnail 设置为 IBOutlet ......以及其他一些东西......希望您对表/集合视图足够熟悉并且可以适应它。
Hope this helps.
希望这可以帮助。
回答by Suragch
Swift
迅速
Set the button image like this:
像这样设置按钮图像:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
where myImageName
is the name of your image in your asset catalog.
myImageName
资产目录中图像的名称在哪里。
回答by Bijender Singh Shekhawat
In one line we can set image with this code
在一行中,我们可以使用此代码设置图像
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
回答by Dilip Tiwari
Code for background image of a Button in Swift 3.0
Swift 3.0 中按钮背景图像的代码
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
Hope this will help someone.
希望这会帮助某人。