Here is how to set a background color in UIimage using Swift Programming Language while developing app for iOS in Xcode. With addition to this here is list of UIColor predefined / preset component values in Xcode that you can use using dot operator

I will explain below how to set background color to UIimage while developing app for iOS.

 

Assumptions: I assume that you have already created Xcode project for iOS app development & you are using storyboards for developing the same. Also you have dragged UIimage View to your current ViewController.

 
Step 01: Now connect your UIimage View to your ViewController.swift file with code something like this

 

@IBOutlet weak var selectImage: UIImageView!

 
Step 02: Now in your desired function set your UIimage’s background color with following code using dot operator

 

    func functionName() {
        
        self.selectImage.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0) // Set background color black using RGB values
        self.selectImage.backgroundColor = UIColor.blackColor() // Set background color black using Xcode predefined values
	
    }

 
With addition to this code following is list UIColor predefined / preset component values in Xcode that you can use using dot operator for your OSX or iOS app development project:

 

    func functionName() {

        // Set background color black using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.blackColor()

        // Set background dark gray using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.darkGrayColor()

        // Set background color light gray using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.lightGrayColor()

        // Set background color white using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.whiteColor()

        // Set background color gray using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.grayColor()

        // Set background color red using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.redColor()

        // Set background color green using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.greenColor()

        // Set background color blue using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.blueColor()

        // Set background color cyan using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.cyanColor()

        // Set background color yellow using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.yellowColor()

        // Set background color magenta using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.magentaColor()

        // Set background color orange using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.orangeColor()

        // Set background color purple using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.purpleColor()

        // Set background color brown using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.brownColor()

        // Set background color clear using Xcode predefined values
        self.selectImage.backgroundColor = UIColor.clearColor()
	
    }

 

Further reading: UIColor Class Reference Guide available on Apple Developer Portal

 
Hope it helps,

 
Thanks & Regards
Mandar Apte

Leave a Reply

Your email address will not be published. Required fields are marked *