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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With