I have a UICollectionView which contains custom UICollectionViewCell. The collection view has a background image.
I would show the background image, but the collection view (or its cells) has always a white background which prevents my background image to show.
Here is the code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AgendaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:agendaCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.layer.borderWidth=0.0f;
cell.layer.backgroundColor = [UIColor clearColor].CGColor;
cell.contentView.layer.backgroundColor = [UIColor blackColor].CGColor;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
[cell setCurrentDateAtIndex:([indexPath row]) date:currentMonth events:events];
return cell;
}
The AgendaCollectionCell is very simple: I did a cell by using IB and set all background images to clearColor. Also the collection view use a very basic custom layout.
An excertp of what is shown in the pic:
The collection view show cells with numbers. Beneath the collection view there is a background which is not shown because of the white color of the cells.
Any hint appreciated. Thank you.
SOLVED.
The problem was in the rendering of the custom cell. I used a method (a lost method...) which set the background color of the cell. For non custom cell, the code in my question is correct.
you can also background color for your custom UICollectionView by setting backgroundview for UICollectionView.
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
self.collectionView.backgroundView = blueView;
Related
Follow this answer on SO I be able to create UICollectionView programmatically. But I can't find any better solution when I try to add subview into UICollectionViewCell. Here is how most answer on SO achieve
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
image.image = /*some image*/
[cell addSubview:image];
return cell;
}
Maybe i'm wrong but the purpose of using UICollectionView wasn't because of recycle and reuse to optimize the performance? If using the code above when the user scrolling wasn't it add more and more UIImageView into UICollectionViewCell subview when dequeueReusableCellWithReuseIdentifier get trigger?
So what is the better way to do this? I can't use UITableView for this because I need the horizontal scrolling technique.
NOTE: I need to create it programmatically without using xib.
Yes, It will add imageView everytime when your collection view will get scrolled! And it is not good approach. Now, what you can do is, Take one image view in your collectionview cell, I mean in interface builder(xib or storyboard whatever you have used) and in cellForItemAtIndexPath just show or hide that image view as per need or change image of it as per requirement!
Update :
If you have create collection view programmatically then you can set some flag in your cellForItemAtIndexPath! Take one flag and set it after adding imageview. and you can declare imageview and flag as global variable. Something like,
if (!isImageAdded) {
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = /*some image*/
[cell addSubview:image];
isImageAdded = YES;
}
else{
imageView.image = /*some image*/
}
I want to only color my left half or right half of my tableview cell background. How can I manage that? For now, I am only able to set the background color for the entire row. I have tried to put a label on the left half of the cell, and set the background color of the label. But the problem is then the label covered the cell.text so that I cannot see the content of the cell. I really want to know if I can set the label just under the cell text but above the cell's background. so it can does its work. But other approach to accomplish just coloring half of the tableview cell is appreciated as well!
I have tried #winnder.ktw to set it up programmatically. however I got an error image how can i fix that??
thank you!
If you're using interface builder, just change the order of the views in the Document Outline panel at the left, and you can choose what's on top of what:
If all you need it the background there's no need to add a label, just add a regular view to your cell.
Edit: here, just add a view and a label, and make sure the label is below the view in the Document Outline panel.
try this
- (UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier: #"any-cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: #"any-cell"];
UIView* bgView = [[UIView alloc] initWithFrame: cell.bounds];
bgView.backgroundColor = [UIColor whiteColor];
UIView* halfView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, cell.bounds.size.width / 2.0, cell.bounds.size.height)];
halfView.backgroundColor = [UIColor greenColor];
[bgView addSubview: halfView];
cell.backgroundView = bgView;
}
return cell;
}
I am trying to simply add a UIView to a UICollectionViewCell that takes up the entire cell's frame. The code I have now only displays a single cell in the top left corner (I suspect each cell is getting layer out on top of each other). How exactly can I do this?
I plan on subclassing UIView later so as to customize the view that I am adding to the cell. I don't want to subclass UICollectionViewCell however based on the way I will be using it.
#pragma mark - Collection view data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIView *menuItem = [[UIView alloc] init];
menuItem.frame = cell.frame;
menuItem.backgroundColor = [UIColor greenColor];
[cell addSubview:menuItem];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; {
return CGSizeMake(60, 60);
}
You'll need to use bounds, not frame. If you don't know the difference between the two or why this matters, you should read up on this and get comfortable with it before going further:
menuItem.frame = cell.bounds;
You'll also want to set an autoresizing mask, since the cell won't initially be at the correct size:
menuItem.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
And, really, you should be adding this to the cell's contentView property:
[cell.contentView addSubview:menuItem];
menuItem.frame = cell.contentView.bounds;
That said, if you plan on adding lots of subviews to menuItem, I recommend subclassing UICollectionViewCell instead of trying to build it in your cellForItemAtIndexPath: method. It will be much easier to control if the layout and setup is encapsulated in a different class, and you can respond to height / width changes by overriding layoutSubviews.
You should only interact with the cell's contentView in this case.
UIView *menuItem = [UIView new];
menuItem.frame = cell.contentView.bounds;
menuItem.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:menuItem];
It's Swift version.
let menuItem = UIView.init()
menuItem.frame = cell.bounds
menuItem.backgroundColor = .green
cell.contentView.addSubview(menuItem)
menuItem.frame = cell.contentView.bounds
Swift 5 Apple doc:
var contentView: UIView
The main view to which you add your cell’s custom content.
When configuring a cell, you add any custom views representing your cell’s content to this view. The cell object places the content in
this view in front of any background views.
var backgroundView: UIView?
The view that is displayed behind the cell’s other content.
Use this property to assign a custom background view to the cell. The background view is placed behind the content view and its frame
is automatically adjusted so that it fills the bounds of the cell.
Example:
let template = UIView()
self.backgroundView = template
template.layer.cornerRadius = 10
template.backgroundColor = .white
This question already has answers here:
iPhone Fixed-Position UITableView Background
(5 answers)
Closed 9 years ago.
I set the backgroundImage of my tableView:
self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"Content.png"]];
And I set transparent the backGroundColor cells of my table View:
SBQCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil){
cell.backgroundColor=[UIColor clearColor];
}
When I do scroll, the backGroundColor of the Table View changes.
Capture 1 (Without Scroll):
Capture 2 (With Scroll):
Thats the image Im using as background:
What can I do to skretch the Image background to the full table View? Thanks
Make the background on a view layer behind the TableView, not on the TableView itself and then set a clear background on the UITableView
or...
Set the tableview.backgroundView not the backgroundColor
Solved with:
self.tableView.backgroundView=[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"ContentBg.png"]];
More:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.backgroundColor = [UIColor clearColor];
}
The cells keep transparent when I do scroll and the image for the backGround of the Table View is resized.
Thanks everyone!
You're adding the background image as a pattern to the backgroundColor property.
Maybe try this:
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Content.png"]];
[self.tableView setBackgroundView:iv];
See the Apple UITableView class reference here:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/backgroundView
You have:
if (cell==nil){
cell.backgroundColor=[UIColor clearColor];
}
I think you mean:
if (cell!=nil) {
cell.backgroundColor=[UIColor clearColor];
}
How do I set the cell selection color for the view controller listed in the More of a tab view controller on iOS 6?
The default is blue, which looks terrible with an app that uses a non-default color. I'd like to set it to a custom color, if possible, but setting it to grey would do.
This problem is specific to iOS 6 because on iOS 7 a grey selection color is used instead.
We can update cell selection color for the view controller listed in the More of a tab view controller on by creating custom datasource for MoreNavigationController.
I have created an sample project which might help - https://github.com/deepthit/CustomizeMoreNavigationController.git
In the custom data source object we can override cellForRowAtIndexPath method to set selectedbackgroundView for a cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
// Set background color
UIView *background = [[UIView alloc] initWithFrame:cell.frame];
background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
background.backgroundColor = [UIColor whiteColor];
cell.backgroundView = background;
// Set selected background color
UIView *selectionColor = [[UIView alloc] initWithFrame:cell.frame];
selectionColor.backgroundColor = [UIColor greenColor];
selectionColor.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.selectedBackgroundView = selectionColor;
return cell;
}