以编程方式设置button背景图像iPhone
在Xcode中,如何将button的背景设置为图像? 或者,如何在button中设置背景渐变?
完整的代码:
+ (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中按下你想要的图像button,然后在右边的实用工具栏中按下属性检查器 ,并将背景设置为你想要的图像! 确保你在左边的支持文件中有你想要的图片。
在表格或集合视图单元格中设置图像时,这对我有用:
将下面的代码放在“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 …和一些其他的东西…希望你已经足够熟悉表/集合视图,可以适应这个。
希望这可以帮助。
迅速
像这样设置button图像:
let myImage = UIImage(named: "myImageName") myButton.setImage(myImage , forState: UIControlState.Normal)
其中myImageName
是您的资产目录中图像的名称。
在一行中,我们可以用这个代码设置图像
[buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
Swift 3.0中 Button的背景图片代码
-
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
希望这会帮助别人。