在Xcode中,如何将
UIButton
的背景设置为图像?或者,如何在UIButton
中设置背景渐变?完整代码:
+ (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:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
如果有帮助的话,setBackgroundImage
对我没用,但是 setImage
有用。
您可以不用任何代码设置背景图片!
只需在Main.storyboard中按下要添加图像的按钮,然后在右侧的属性检查器中按下属性并将背景设置为所需的图像!请确保您想要的图片位于左侧的支持文件中。
Swift
按照以下方式设置按钮图像:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
其中myImageName
是您资产目录中图像的名称。
当在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...还有其他一些东西...希望你对表格/集合视图足够熟悉,可以将其整合进去。
希望这能帮到你。
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
Swift 3.0中设置按钮背景图的代码:
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
希望能对大家有所帮助。