gpt4 book ai didi

ios - 更改 ViewController Swift 的背景颜色? (单 View 应用程序)

转载 作者:IT王子 更新时间:2023-10-29 05:13:05 25 4
gpt4 key购买 nike

我正在使用 Swift (XCode 6.2) 制作一个非常简单的单 View 应用程序,它包含 2 个按钮“blackButton”和“whiteButton”。单击 blackButton 后,它会将 View 的背景颜色更改为黑色,单击 whiteButton 后,它会将背景更改为白色。谁能建议任何可能的方法来做到这一点?

ViewController.swift:

//beginning
import UIKit

class ViewController: UIViewController {

@IBAction func blackButton(sender: AnyObject) {
}
@IBAction func whiteButton(sender: AnyObject) {
}
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.
}


}

最佳答案

可以通过它的 view 属性访问 View Controller 的 View ,这只是一个普通的 UIViewUIView 有一个 backgroundColor 属性,它是一个 UIColor 并控制 View 的颜色。

@IBAction func blackButton(sender: AnyObject) {
view.backgroundColor = .black
}

@IBAction func whiteButton(sender: AnyObject) {
view.backgroundColor = .white
}

关于ios - 更改 ViewController Swift 的背景颜色? (单 View 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com