Ios 如何在swift3中为视图添加颜色?

Ios 如何在swift3中为视图添加颜色?,ios,Ios,这就是上面代码的问题,我在swift3中更改视图的背景色时遇到了问题。我在xcode编译器中得到的错误是,“无法调用非函数类型'UIColor'的值”。谁能帮我想出解决这个问题的办法。是否有一个函数可以获取背景颜色的颜色?试试看 let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150) let firstView = UIView(frame: firstFrame) firstView.backgr

这就是上面代码的问题,我在swift3中更改视图的背景色时遇到了问题。我在xcode编译器中得到的错误是,“无法调用非函数类型'UIColor'的值”。谁能帮我想出解决这个问题的办法。是否有一个函数可以获取背景颜色的颜色?

试试看

    let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150)
    let firstView = UIView(frame: firstFrame)
    firstView.backgroundColor = UIColor.blueColor()
    view.addSubview(firstView)

    let secondFrame = CGRect(x: 20, y: 30, width: 50, height: 50)
    let secondView = UIView(frame: secondFrame)
    secondView.backgroundColor = UIColor.greenColor()
    view.addSubview(secondView)

如果您在Swift 3中工作,则应如下所示:

UIColor.blue
UIColor.green

实际上你可以这样说

let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150)
let firstView = UIView(frame: firstFrame)
firstView.backgroundColor = UIColor.blue
view.addSubview(firstView)

let secondFrame = CGRect(x: 20, y: 30, width: 50, height: 50)
let secondView = UIView(frame: secondFrame)
secondView.backgroundColor = UIColor.green
view.addSubview(secondView)

不需要说“UIColor”;编译器知道背景色是一种颜色。你只要告诉它什么颜色。

它现在可以工作了。错误不再出现了。您可以自己轻松地解决这些错误⌘-单击符号(例如在
UIColor
)。
firstView.backgroundColor = .blue