在Swift,Xcode 6中更改backgroundColor [英] Changing backgroundColor in Swift, Xcode 6

查看:201
本文介绍了在Swift,Xcode 6中更改backgroundColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在iOS中更改backgroundColor的代码在某种程度上是这样的:

I know the code to change the backgroundColor in iOS is something to this extent:

self.view.backgroundColor = UIColor.yellowColor()

但是由于某种原因,我收到意外声明"错误.

But for some reason, I am getting an "Unexpected declaration" error.

有人知道为什么吗?

这是我正在处理的文件ViewController.swift.它实际上只是Xcode 6中的一个新的单视图应用程序模板.

This is the file I am dealing with, ViewController.swift. It is literally just a new single-view app template from Xcode 6.

//
//  ViewController.swift
//  backgroundColor
//
//  Created by Frank Barrett on 11/21/14.
//  Copyright (c) 2014 Frank Barrett. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

self.view.backgroundColor = UIColor.yellowColor()

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

错误是

/Users/frank/Dropbox/code/ios/backgroundColor/backgroundColor/ViewController.swift:13:5:需要?>声明

/Users/frank/Dropbox/code/ios/backgroundColor/backgroundColor/ViewController.swift:13:5: Expected ?>declaration

推荐答案

您将代码放在错误的位置.

You put your code in the wrong place.

class ViewController: UIViewController {

    // This space is only for declarations.

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Put this setup code in the viewDidLoad method.
        self.view.backgroundColor = UIColor.yellowColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这篇关于在Swift,Xcode 6中更改backgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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