2015-06-09 249 views
1

我想更改視圖的文字和背景顏色。引號正在改變,但視圖背景顏色不會改變。我究竟做錯了什麼?更改視圖背景顏色

下面是代碼:

import UIKit  // UI: user interface 

class ViewController: UIViewController { 

// IB: Interface Builder 
@IBOutlet weak var quoteLabel: UILabel! 

var quotes = Quotes() 


// gets called when the view is loaded 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

// Interface Builder Action. 
// Gets called whenever the user taps on the button 
@IBAction func inspireMeDidTap(sender: UIButton) 
{ 
    let quote = quotes.randomQuote() 
    quoteLabel.text = quote 

    // change the background color of the view 
    view.backgroundColor = randomColor() 
} 

func randomColor() -> UIColor 
{ 
    let random = Int(arc4random()) % 5 // 0 -> 4 
    switch random { 
    case 0: return UIColor(red: 211/255.0, green: 86/255.0, blue: 87/255.0, alpha: 0) 
    case 1: return UIColor(red: 71/255.0, green: 178/255.0, blue: 137/255.0, alpha: 0) 
    case 2: return UIColor(red: 229/255.0, green: 177/255.0, blue: 93/255.0, alpha: 0) 
    case 3: return UIColor(red: 92/255.0, green: 163/255.0, blue: 178/255.0, alpha: 0) 
    case 4: return UIColor(red: 38/255.0, green: 38/255.0, blue: 38/255.0, alpha: 0) 
    default: return UIColor(red: 56/255.0, green: 72/255.0, blue: 72/255.0, alpha: 0) 
    } 

} 

} 
+0

不要發佈你的代碼的圖片。用相關代碼的實際文本更新您的問題。確保它的格式也正確。 – rmaddy

+0

對不起。我改變了它。 –

+0

你的代碼工作正常,我沒有看到任何問題。 –

回答

0

要設置阿爾法參數爲零則意味着任何顏色是這將是完全透明單位(非白色)。

所以你需要做的只是設置alpha爲1,你會得到你的正確顏色