Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView background Color

Tags:

iphone

uiview

I am trying to create a grid view by using a UITableView (my older questions have pointed me in this direction) and am currently setting up the views for the individual items. Having created a custom UITableViewCell that displays 3 items per row, I have decided to pull these items into a subclass of UIView called ItemView.

This ItemView will then be added as a subview to the custom UITableViewCell to display the grid. Anyway, I have managed to create the view and can get it to display a UILabel fine, however I am having trouble in changing ItemView to be transparent apart from the UIViews (Labels, buttons, images etc) within it. Here is my code for the UIView:

#import <UIKit/UIKit.h>
@interface ItemView : UIView {
}
@end

#import "ItemView.h"
@implementation ItemView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
  [self setBackgroundColor:[UIColor lightGrayColor]];
}

- (void)dealloc {
  [super dealloc];
}
@end

Where should I set the background color for it to work properly?

Cheers

like image 809
Jack Avatar asked Feb 16 '10 22:02

Jack


People also ask

How do I change the background color in view controller?

First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below.

How do I change the background color in Xcode?

backgroundColor = [UIColor redColor] ; is the code to change the background color. You get the color name before "Color" from the names of colors in the Apple color palette.

How do I change the RGB color in Swift?

In Objective-C, we use this code to set RGB color codes for views: #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] view.


1 Answers

Try:

self.backgroundColor = [UIColor clearColor];

I don't see any reason not to have that in your init method. I don't think you want to override drawRect: in this case as you'll need to draw the entire view yourself.

like image 73
Rob Jones Avatar answered Oct 02 '22 19:10

Rob Jones