如何在swift3中为视图添加颜色? [英] How to add color to a view in swift3?

查看:118
本文介绍了如何在swift3中为视图添加颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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)

这是上面的代码我在更改swift3中的视图背景颜色时出现问题。我在xcode编译器中得到的错误是无法调用非函数类型的值'UIColor'。任何人都可以帮我找到解决这个问题的方法。是否有一个为backgroundColor抓取颜色的函数?

This is the question the code above I'm having problem with changing the background color for view in swift3. The error that I get in the xcode compiler is, "cannot call value of non-function type 'UIColor'". Can anyone help me figure a solution to this problem. Is There a function that grabs a color for backgroundColor?

推荐答案

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

If you working in Swift 3 it should be as below:

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)

这篇关于如何在swift3中为视图添加颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆