2014-11-20 60 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

相關問題