2011-10-04 170 views
1

我在滾動視圖內創建了一個自定義UIButton。 但圖片不顯示,有什麼想法我做錯了什麼?iphone自定義UIButton不顯示圖片

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [button setBackgroundImage:[UIImage imageNamed:@"anzahl.png"] forState:UIControlStateNormal]; 
       [button setTitle:@"-" forState:UIControlStateNormal]; 
       button.frame = CGRectMake(x, y, viewWidth, viewHeight);//width and height should be same value 
       button.clipsToBounds = YES; 
       button.layer.cornerRadius = 20;//half of the width 
       button.layer.borderColor=[UIColor blueColor].CGColor; 
       button.layer.borderWidth=1.0f; 
       [button setAlpha:1.0]; 
       [scroller1 addSubview:button]; 
       [button release]; 
+0

你傳遞了​​正確的幀? – Hisenberg

+0

我自己對iOS有點新,但是如果它不是alloc/init-ed,你應該釋放按鈕嗎? – Gregir

+0

偉大的:))這是問題,當我釋放它沒有顯示。我首先用alloc創建了它,後來將其替換。謝謝問題解決 – Ferdi

回答

1

您不需要釋放按鈕,因爲它沒有被分配。你已經在UIButton上調用了一個類方法,所以不需要發佈。

我還建議你先確認圖像被正確加載,因此,測試

UIImage* backgroundImage = [UIImage imageNamed:@"anzahl.png"]; 
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal]; //breakpoint and ensure that backgroundImage is not nil (0x0) 

或者你可以有你的設置框座標不正確,因此該按鈕被繪製可視屏幕面積之外,所以下一步是確保您的viewWidth & viewHeight是正確的大小,而不是0.還要確保您的協調器位於屏幕的當前可見區域,而不是在可見屏幕邊界之外(請記住, x,y位置將相對於它們所放置的超級視圖(在本例中爲scroller1))。

+0

是釋放是我的問題謝謝你:) – Ferdi