我想创建一个带有自定义图像(背景图像)的按钮,我已经这样做了。 如何将imageview分配给may按钮?
感谢您的回答
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:@"ico_plan.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
答案 0 :(得分:7)
你想要的是:
[button setBackgroundImage:image forState:UIControlStateNormal];
或
[button setImage:image forState:UIControlStateNormal];
答案 1 :(得分:6)
@izan您不需要在UIImageView
上显示单独的UIButton
。
UIButton
里面有UIImageView
个setImage:forState:
。你可以为这两个imageViews设置图像。
您可以使用setBackgroundImage:forState:
方法为第一个图像视图设置图像。通过这样做,您无法显示该按钮的任何标题。如果要在按钮上显示标题和图像,则应使用{{1}}方法。
答案 2 :(得分:5)
使用setImage方法;
- (void)setImage:(UIImage *)image forState:(UIControlState)state
所以你的代码就是;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:@"ico_plan.png"];
[button setImage:image forState:UIControlStateNormal];
或者我仍然希望您的文字显示用途;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
哪会导致;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:@"ico_plan.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
UIButton文档; http://bit.ly/kruq5y
答案 3 :(得分:0)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"ico_plan.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
希望这有帮助