Asked 7 years ago
4 Jul 2017
Views 1079
fatso

fatso posted

how to set background to view in iOS ?

how to set background to view in iOS ?
ravi

ravi
answered Apr 24 '23 00:00

To set the background color of a UIView in iOS, you can use the backgroundColor property. This property is an instance of the UIColor class and is used to set the background color of the view.

Here's an example of how to set the background color of a UIView to red:



UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
myView.backgroundColor = [UIColor redColor];

In this example, we first create a UIView instance with a frame of (0, 0, 200, 200). Then we set the background color of the view to red using the backgroundColor property and the class method redColor of the UIColor class.

You can also set the background color to a custom color using the UIColor initializer method initWithRed:green:blue:alpha:. Here's an example:


UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
myView.backgroundColor = [[UIColor alloc] initWithRed:0.2 green:0.3 blue:0.4 alpha:1.0];

In this example, we set the background color of the view to a custom color with red, green, and blue components of 0.2, 0.3, and 0.4, respectively, and an alpha value of 1.0.

Note that you can also set the backgroundColor property of a UIView object using Interface Builder by selecting the view and changing the background color in the Attributes Inspector.
Post Answer