簡體   English   中英

在水龍頭上更改視圖BackgroundColor

[英]Change the View BackgroundColor on Tap

我想更改點擊時視圖的文本和背景顏色。 引號在變化,但視圖背景顏色不變。 我究竟做錯了什么?

這是代碼:

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)
    }

}

}

嘗試這個:

將所有alpha = 1設置為您的顏色(而不是0)。 如果alpha = 0,則完全透明

alpha的含義

您將alpha參數設置為零,這意味着無論顏色是什么,它將完全透明(不是白色)。

因此,您需要做的就是將alpha設置為1,您將獲得正確的顏色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM