2014-11-20 58 views
0

我正在使用UIButton作为链接,theres一个更改将没有图像,所以我已设置为0高度,但其宽松的优先级,所以当我添加图像时它会扩大。问题是可点击区域不会随着图像的添加而扩大。以编程方式添加图像后更改UIButton点击区域

目前我试图添加像这样的图像。

[self.advertisementButton sd_setBackgroundImageWithURL:[NSURL URLWithString:url] 
                forState:UIControlStateNormal]; 
    [self.advertisementButton setFrame:self.advertisementButton.imageView.bounds]; 
    [self.advertisementButton layoutIfNeeded]; 
+0

你设置'userInteractionEnabled = YES'你的'UIImageView'? – 2014-11-20 18:49:59

+0

它是一个UIButton,它只在我点击图像的顶部时才起作用。 – plaidpancakes 2014-11-20 18:53:37

+0

'sd_setBackgroundImageWithURL'做什么? – 2014-11-20 18:56:38

回答

0

首先你下载你的图像,然后设置你的图像大小的按钮帧。 这里你正在服用的按钮的imageView属性设置button frame

// Download like 
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"imageurl"]]; 
    UIImage *image = [UIImage imageWithData: imageData]; 

// Set frame like 
    [advertisementButton setFrame:CGRectMake(advertisementButton.frame.origin.x, advertisementButton.frame.origin.y, image.size.width , image.size.height)]; 
+0

不适用于我...同样的问题,只是可点击的原始范围 – plaidpancakes 2014-11-21 15:14:03

相关问题