【问题标题】:Change the View BackgroundColor on Tap更改点击时查看背景颜色
【发布时间】:2015-08-24 12:55:45
【问题描述】:

我想在点击时更改视图的文本和背景颜色。引号正在更改,但视图背景颜色不会更改。我做错了什么?

代码如下:

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

}

}

【问题讨论】:

  • 不要发布您的代码图片。使用相关代码的实际文本更新您的问题。确保它的格式也正确。
  • 对不起。我已经改变了。
  • 您的代码运行良好,我没有发现任何问题。

标签: ios swift view uikit background-color


【解决方案1】:

试试这个:

将所有 alpha = 1 放入您的颜色中(而不是 0)。 如果 alpha = 0,则完全透明

Meaning of alpha

【讨论】:

    【解决方案2】:

    您将 alpha 参数设置为零,这意味着无论颜色是什么,它将是完全透明的(不是白色)。

    所以您只需将 alpha 设置为 1 即可获得正确的颜色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2019-09-01
      • 2014-06-12
      • 2020-11-28
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多