Ios 以编程方式设置按钮背景图像
在Xcode中,如何将Ios 以编程方式设置按钮背景图像,ios,iphone,xcode,image,button,Ios,Iphone,Xcode,Image,Button,在Xcode中,如何将ui按钮的背景设置为图像?或者,如何在ui按钮中设置背景渐变?完整代码: + (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector frame:(CGRect)frame i
ui按钮的背景设置为图像?或者,如何在ui按钮中设置背景渐变?完整代码:
+ (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];
要删除图像,请执行以下操作:
UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
[button setBackgroundImage:nil forState:UIControlStateNormal];
如果有人setBackgroundImage
对我不起作用,但setImage
对我有帮助,这将起作用
UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
你可以设置一个没有任何代码的背景图像
只需在Main.storyboard中按您希望图像显示的按钮,然后在右侧的实用程序栏中,按属性检查器并将背景设置为您想要的图像!
确保您在左侧的支持文件中有所需的图片。在表格视图单元格
或集合视图单元格
中设置图像时,这对我很有效:
将以下代码放入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
按如下方式设置按钮图像:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
其中myImageName
是资产目录中图像的名称 在一行中,我们可以使用此代码设置图像
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
Swift 3.0中按钮的背景图像代码
buttonName.setBackgroundImage(UIImage(名为:“facebook.png”),用于:。正常)
希望这将对某人有所帮助。使用setImage设置前景图像。。。如果要设置背景图像,请使用setBackgroundImage:forState:为什么在将按钮的背景设置为该图像之前要创建图像的副本?如果我们不这样做,是否会有内存泄漏或异常行为的风险,还是仅仅为了让图像拉伸以填充按钮?