2014-06-05 79 views
58

當我嘗試創建一個按鈕,並設置在斯威夫特背景圖片:創建一個按鈕編程和設置背景圖片

let button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
    button.frame = CGRectMake(100, 100, 100, 100) 
    button.setImage(IMAGE, forState: UIControlState.Normal) 
    button.addTarget(self, action: "btnTouched:", forControlEvents: UIControlEvents.TouchUpInside) 

    self.view.addSubview(button) 

我總是得到一個錯誤:「無法轉換表達式的類型‘無效’來鍵入'UIImage'「。

+0

你能顯示代碼,您聲明'IMAGE'? – 68cherries

+2

你是如何創建圖像的?可能需要解開它的圖像! –

+1

'圖像'應該看起來像'UIImage(名稱:「名稱」)' – Daniel

回答

116

你的代碼看起來應該像下面

let image = UIImage(named: "name") as UIImage? 
let button = UIButton(type: UIButtonType.Custom) as UIButton 
button.frame = CGRectMake(100, 100, 100, 100) 
button.setImage(image, forState: .Normal) 
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside) 
self.view.addSubview(button) 
+2

我認爲它應該實際上是'as? UIImage'如果'imageNamed:'返回nil,你的版本會崩潰。 –

+3

那麼這是你的錯,如果它的零,但你可以添加?但是當'setImage'的時候你也必須注意到這一點。我想這取決於你想要得到多少防守,既然你控制了當地的資產,那麼在單元測試或者兩個單元測試中應該沒有什麼意外。 –

+1

對於圈子圖像怎麼樣? – User

19

如下嘗試:

let image = UIImage(named: "ImageName.png") as UIImage 
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
button.frame = CGRectMake(100, 100, 100, 100) 
button .setBackgroundImage(image, forState: UIControlState.Normal) 
button.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside) 
menuView.addSubview(button) 

讓我知道,如果它工作或沒有?

+0

您應該將buttonWithType設置爲UIButtonType.Custom,而不是System,直到圖像顯示 – sazz

4

您需要檢查圖像不是零,然後將其分配給按鈕。

例子:

let button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
button.frame = CGRectMake(100, 100, 100, 100) 
if let image = UIImage(named: "imagename.png") { 
    button.setImage(image, forState: .Normal) 
} 

button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside) 
self.view.addSubview(button) 
+0

我會認爲我會想要應用程序崩潰,如果圖像不在項目中。那麼爲什麼要檢查? – Jeff

+0

因爲應用程序不會崩潰,並且可能會丟失圖像 – MacInnis

3
let btnRight=UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
btnRight.frame=CGRectMake(0, 0, 35, 35) 
btnRight.setBackgroundImage(UIImage(named: "menu.png"), forState: UIControlState.Normal) 
btnRight.setTitle("Right", forState: UIControlState.Normal) 
btnRight.tintColor=UIColor.blackColor() 
0

斯威夫特:UI按鈕編程創建

let myButton = UIButton() 

myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500) 

myButton.titleLabel!.text = "Button Label" 

myButton.titleLabel!.textColor = UIColor.redColor() 

myButton.titleLabel!.textAlignment = .Center 

myButton.addTarget(self,action:"Action:",forControlEvents:UIControlEvent.TouchUpInside) 

self.view.addSubview(myButton) 
3

這是你如何能與邊框創造一個美好的按鈕,圓滑的邊緣:

loginButton = UIButton(frame: CGRectMake(self.view.bounds.origin.x + (self.view.bounds.width * 0.325), self.view.bounds.origin.y + (self.view.bounds.height * 0.8), self.view.bounds.origin.x + (self.view.bounds.width * 0.35), self.view.bounds.origin.y + (self.view.bounds.height * 0.05))) 
loginButton.layer.cornerRadius = 18.0 
loginButton.layer.borderWidth = 2.0 
loginButton.backgroundColor = UIColor.whiteColor() 
loginButton.layer.borderColor = UIColor.whiteColor().CGColor 
loginButton.setTitle("Login", forState: UIControlState.Normal) 
loginButton.setTitleColor(UIColor(red: 24.0/100, green: 116.0/255, blue: 205.0/205, alpha: 1.0), forState: UIControlState.Normal) 
3

The convenience initializer for UIButton has changed for swift 2.0 (iOS9, Xcode 7)

convenience init(type buttonType: UIButtonType) 

Create Button and add image as its background swift 3.0 (iOS10, Xcode 8.3).

let image = UIImage(named: "imgname") as UIImage? 
    let button = UIButton(type: UIButtonType.system) 
    button.frame = CGRect.init(x: 10, y: 10, width: 100, height: 45) 
    button.setImage(image, for: .normal) 
    self.view.addSubview(button) 
+0

糾錯:第4行] button.setImage(image,for:.normal ) – MacInnis

6

SWIFT 3亞歷克斯·雷諾的答案版本

let image = UIImage(named: "name") as UIImage? 
let button = UIButton(type: UIButtonType.custom) as UIButton 
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100) 
button.setImage(image, for: .normal) 
button.addTarget(self, action: Selector("btnTouched:"), for:.touchUpInside) 
     self.view.addSubview(button) 
0

要設置背景下,我們可以不再使用forState,所以我們必須使用for設置UIControlState

let zoomInImage = UIImage(named: "Icon - plus") as UIImage? 
let zoomInButton = UIButton(frame: CGRect(x: 10), y: 10, width: 45, height: 45)) 
zoomInButton.setBackgroundImage(zoomInImage, for: UIControlState.normal) 
zoomInButton.addTarget(self, action: #selector(self.mapZoomInAction), for: .touchUpInside) 
self.view.addSubview(zoomInButton) 
+1

請解釋一下這段代碼的作用 –

+0

我使用這段代碼在谷歌地圖上放置縮放按鈕,使用上面的代碼設置背景圖片,它與之前的不一樣 – Shazoo

+0

雖然它與其他答案不同嗎? –

0

更新到斯威夫特3

let image = UIImage(named: "name") 
let button = UIButton(type: .custom) 
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100) 
button.setImage(image, for: .normal) 
button.addTarget(self, action: #selector(self.btnAbsRetClicked(_:)), for:.touchUpInside) 
self.view.addSubview(button)