Change Background Color of ViewController Swift? (Single View Application)

MarcB1

I am making a very simple single view application in Swift (XCode 6.2) that comprises of 2 buttons "blackButton" and "whiteButton". Upon clicking blackButton it changes the View's background color to Black and upon clicking the whiteButton it changes the background to white. Can anyone suggest any possible ways to do this?

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.
    }


}
nhgrif

A view controller's view can be accessed through it's view property, which is just a regular UIView. UIView's have a backgroundColor property, which is a UIColor and controls the color of the view.

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change the background color of a Navigation View in Swift 5

How to change background view color partialy in ios swift

iOS Swift - How to change background color of Table View?

Swift: Setting ViewController background color from AppDelegate

Background color not change TextEditor view

Gradually change background color in Swift

Changing View Background color of a single row

Change the background color of a single chat bubble dynamically

Android - Change the background color of a single item in the NavigationView

Android Change background color of single item in NavigationView

Is it possible to change the background color of a single cell in ParseQueryAdapter?

change background color single item menu

Change background color of a view without removing drawable

Animate change of view background color on Android

How to use animatorSet to change background color of a View

Change Recycler view item background color by code

Unable to change background color of view in SwiftUI

How to change AlertDialog view background color?

change background color of view by its ID in Titanium

How to change the background of the navigation bar of a single view?

Swift iOS Background Color View animation as Timer

Customise the navigation bar background for a single view in Swift?

Xamarin.Forms Change Background Color For Application

Change Status Bar Background Color in Swift 3

How to change background color of UiSearchBar to black -SWIFT

Swift: Background color change in selected segment of UISegmentControl

How to change programmatically the background color of in tvOS in Swift?

Change Background Color of UITableViewCells when Switch is on - Swift

Change button background color when selected SWIFT