将图像添加到我的 UIButton 子视图
Adding an Image to my UIButton subview
我有一个 UIButton
作为子视图添加到我的超级视图中,它只是一个大块,如何向我的按钮添加图像。
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
使用这个:
-setBackgroundImage:forState:
如果您想要一个 "image as button" 而不仅仅是一个带有图像的默认按钮,您可能会寻找这样的东西
BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
CGRect frame;
//universal app
if (ipad)
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
else
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
btn.titleLabel.backgroundColor = [UIColor clearColor];
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
有多种方法可以做到这一点。
第一个是您可以将图像视图添加到您的超级视图并根据需要设置图像视图的图像。然后,在同一位置添加您的按钮,其大小为 imageview,并将按钮颜色设置为 clearColor。但是您需要先添加 imageView 否则您的按钮将位于图像下方。
第二个是按钮的默认图片,使用setImage:imageforState:UIControlStateNormal或setBackgroundImage:imageforState:UIControlStateNormal.
知道了,方法如下:
[button setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
您可以通过以下两种方式添加image in UIButton
。
1) 借助设置 Background Image
.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
2) 或者您可以将 UIImageView
添加为 subview
。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setClipsToBounds:YES];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
UIImageView * imgView = [[UIImageView alloc]init];
[imgView setImage:[UIImage imageNamed:@""]];
[imgView setFrame:CGRectMake(0, 0, 100, 100)];
[button addSubview:imgView];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
使用this method。
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
我有一个 UIButton
作为子视图添加到我的超级视图中,它只是一个大块,如何向我的按钮添加图像。
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
使用这个: -setBackgroundImage:forState:
如果您想要一个 "image as button" 而不仅仅是一个带有图像的默认按钮,您可能会寻找这样的东西
BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
CGRect frame;
//universal app
if (ipad)
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
else
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
btn.titleLabel.backgroundColor = [UIColor clearColor];
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
有多种方法可以做到这一点。 第一个是您可以将图像视图添加到您的超级视图并根据需要设置图像视图的图像。然后,在同一位置添加您的按钮,其大小为 imageview,并将按钮颜色设置为 clearColor。但是您需要先添加 imageView 否则您的按钮将位于图像下方。
第二个是按钮的默认图片,使用setImage:imageforState:UIControlStateNormal或setBackgroundImage:imageforState:UIControlStateNormal.
知道了,方法如下:
[button setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
您可以通过以下两种方式添加image in UIButton
。
1) 借助设置 Background Image
.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
2) 或者您可以将 UIImageView
添加为 subview
。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setClipsToBounds:YES];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
UIImageView * imgView = [[UIImageView alloc]init];
[imgView setImage:[UIImage imageNamed:@""]];
[imgView setFrame:CGRectMake(0, 0, 100, 100)];
[button addSubview:imgView];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
使用this method。
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state