ios – 更改ViewController Swift的背景颜色? (单视图应用程序)

前端之家收集整理的这篇文章主要介绍了ios – 更改ViewController Swift的背景颜色? (单视图应用程序)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Swift(XCode 6.2)中制作了一个非常简单的单视图应用程序,它包含2个按钮“blackButton”和“whiteButton”.点击blackButton后,将View的背景颜色更改为Black,单击whiteButton后,将背景更改为白色.任何人都可以提出任何可能的方法来做到这一点吗?

ViewController.swift:

  1. //beginning
  2. import UIKit
  3.  
  4. class ViewController: UIViewController {
  5.  
  6. @IBAction func blackButton(sender: AnyObject) {
  7. }
  8. @IBAction func whiteButton(sender: AnyObject) {
  9. }
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. // Do any additional setup after loading the view,typically from a nib.
  13. }
  14.  
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19.  
  20.  
  21. }

解决方法

视图控制器的视图可以通过它的view属性访问,它只是一个常规的UIView. UIView有一个backgroundColor属性,它是一个UIColor并控制视图的颜色.
  1. @IBAction func blackButton(sender: AnyObject) {
  2. self.view.backgroundColor = UIColor.blackColor()
  3. }
  4.  
  5. @IBAction func whiteButton(sender: AnyObject) {
  6. self.view.backgroundColor = UIColor.whiteColor()
  7. }

猜你在找的iOS相关文章