I have implemented one row-multiple column collectionview which stays at the top of the HomeViewController.
Initially, I want to set a default item as being selected and highlighted with a red line color beneath and also as well as text color red, refer to the image as follows.
For some reason(s), it shows multiple category items have red line colors beneath which is not what I expected, but text colors are correct. I do not know what I am doing wrong?
CategoryCollectionViewCell.m
#implementation CategoryCollectionViewCell
#synthesize categoryLabel,highlightedLabel;
- (void)setSelected:(BOOL)selected
{
if(selected)
{
self.categoryLabel.textColor = [UIColor redColor];
self.highlightedLabel.backgroundColor = [UIColor redColor];
}
else
{
self.categoryLabel.textColor = [UIColor lightGrayColor];
self.highlightedLabel.backgroundColor = [UIColor clearColor];
}
}
#end
HomeViewController.m
-(void) viewWillAppear: (BOOL) animated {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
[self loadFromURL]; // that is not related with categoryCollectionView
dispatch_async(dispatch_get_main_queue(), ^{
[self.categoryCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
}
});
}
You didn't post all relevant code here.
From your code there may be reason :
1) Your highlighted label color is set to default red in cell. When you pass your code changes that cell only.
For this set your highlighted label background color clear in xib/storyboard.
Related
I am trying to achieve the same highlight that a UIButton background image has, but for a UIButton with no background image. This is what it looks like when the UIButton with an image background has been highlighted.
However, I am unsuccessful achieving this when using a non background image button. This is the code I have so far for my custom button.
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted) {
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.9f];
self.titleLabel.alpha = 0.9f;
} else {
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:1.0f];
self.titleLabel.alpha = 1.0f;
}
[super setHighlighted:highlighted];
}
The main problem is it isn't dark enough. Is there a way I can change the background color and label color to resemble a UIButton with an image background that has been highlighted?
Edit: I know alpha makes it lighter, but this was recommend in a thread when I was researching.
Edit: I created this method
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted) {
if (!overlayView) {
overlayView = [[UIView alloc] initWithFrame:self.bounds];
overlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}
[self addSubview:overlayView];
} else {
[overlayView removeFromSuperview];
}
[super setHighlighted:highlighted];
}
This seems to work well, but not sure if there is a better alternative.
This question has been asked few times but there don't seem to be any solution to this.
UITableView reorder hides background
Subviews of UITableViewCell are not visible while reordering
Reordering causing subview 's backround color to clear
I have a custom tableview cell with UILabel in it. When tableview is in edit mode and I drag the cell to reorder, UILabel's background becomes clear color. I've also found that, if I try to reorder selected cell (my tableview is allowed multiple selection during edit mode), the subview's background color stays.
I tried below methods in my CustomCell but none of them overrides the subview's background color when cell is being dragged.
I want the subview's background color to stay. Is there any method that I missed? Or Apple designed it this way?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
Adding a modern solution for Swift 3.
Create a new .swift file and make a custom UIView class:
import UIKit
class NeverClearView: UIView {
override var backgroundColor: UIColor? {
didSet {
if backgroundColor?.cgColor.alpha == 0 {
backgroundColor = oldValue
}
}
}
}
In Interface Builder, go to the Identity Inspector and set the class to your shiny new NeverClearView class. This will halt it from disappearing during cell reordering.
This solution also works for other UIKit components, for example I had to do the same thing for a UIStepper a second ago.
You may create your custom UILabel. And overload -drawRect.
#interface VALAbel:UILabel
#property (nonatomic, strong) UIColor *bac_color;
#end
#implementation VALabel
-(void)setBac_color:(UIColor *)bac_color
{
_bac_color = bac_color;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.bac_color set];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);
[super drawRect:rect];
}
#end
This will help you!
I have a question about table view cells.
In cell I have an UIImageView. This view loads from web service with getter:
- (UIImageView *)imageView
{
if (!_ imageView)
{
_imageView = [[TurnipImageView alloc] init];
_imageView.backgroundColor = [UIColor clearColor];
}
[_imageView loadImageViewFromWebService];
return _imageView;
}
When I add [_imageView loadImageViewFromWebService]; call inside of if (!_ imageView) statement, image view is not loaded correctly.
When I scroll table view, cells are reloading as well as image views and causing lags in scrolling.
Maybe anyone knows how to optimise this process?
You should try lazy loading of the table view :
here is the sample : https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
also have a look at this :
Lazy loading UITableView with multiple images in each cell
Try this
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
//perform download on background thread
dispatch_async(dispatch_get_main_queue(), ^{
//assign image to imageview on main thread, UI operations should be carried on main thread
});
});
- (UIImageView *)imageView
{
if (!_ imageView)
{
_imageView = [[TurnipImageView alloc] init];
_imageView.backgroundColor = [UIColor clearColor];
}
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
//perform download on background thread
[_imageView loadImageViewFromWebService];
});
return _imageView;
}
Hope this helps
I've got a UILabel with a background color in a cell. When I select this cell, the cell changes the color (which it should) but it also changes the background of the label. I want the preserve the background color on the UILabel. When I use an image with just a random color in it it is preserved, but isn't there any better way?
Thanks in advance
Code:
_label = [UILabel new];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.font = [UIFont systemFontOfSize:10.f];
_label.backgroundColor = HEXCOLOR(0xFFE5E5E5); //Macro just a UIColor
But I use this way to add a different selection color (could have something to do with it)
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = HEXCOLOR(0XFFF1F1F1);
self.selectedBackgroundView = selectionColor;
self.contentView.backgroundColor = [UIColor whiteColor];
Nothing really more to it. Just a simple label added with autolayout to fill with a padding of 5.
Solution:
Create a subclass of UILabel and just not call super
- (instancetype) initWithColor:(UIColor *)color
{
self = [super init];
if (self) {
[super setBackgroundColor:color];
}
return self;
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
//do nothing here!
}
The default behavior of UITableView is that when a cell is selected the background color of all the cell's subviews is temporarily removed.
We usually handled this issue by subclassing UILabel, overwrite setBackgroundColor: and simply do not call [super setBackgroundColor:] after we've set our own color.
#interface MyLabel : UILabel
#property(nonatomic) BOOL backgroundColorLocked;
#end
#implementation MyLabel
-(void) setBackgroundColor:(UIColor*)backgroundColor {
if (_backgroundColorLocked) {
return;
}
super.backgroundColor = backgroundColor;
}
#end
Usage:
MyLabel* label = …;
label.backgroundColor = UIColor.redColor;
label.backgroundColorLocked = YES;
As long as backgroundColorLocked is YES no-one, not even UITableView(Cell), can change the label's background color.
I set up a table view in Interface Builder, with Separator set to Single Line, and in the data source class I have this code:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIImageView *topCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"grouped-cell-bg-top"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]];
cell.backgroundView = topCellBackgroundImageView;
}
// If it's the last row
else if (indexPath.row == ([tableView numberOfRowsInSection:0] - 1)) {
UIImageView *bottomCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"grouped-cell-bg-bottom"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]];
cell.backgroundView = bottomCellBackgroundImageView;
}
else {
UIImageView *middleCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"grouped-cell-bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(3.0, 3.0, 3.0, 3.0)]];
cell.backgroundView = middleCellBackgroundImageView;
}
}
And in viewDidLoad I do:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor redColor];
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithRed:245/255.0 green:244/255.0 blue:240/255.0 alpha:1.0];
}
But the border color never shows up. I tried setting them in the images, but obviously that presents a lot of centering issues with the images, and having their borders overlap.
UPDATE:
After looking around a bit more, it does indeed get set, but it seems that setting the cell's backgroundView puts what you set as the backgroundView over the separatorColor. When I remove the willDisplayCell: method, the red color shows up fine (same when I select the cell even with the backgroundView set, it will show red until I unselect it). The question is, how can I set the separatorColor if I have a backgroundView set?
One trick you can use is to add a UIView to your background view with a red background color and height 1 point. Another option is to ask your designer to incorporate such a red line in the image file for the background view.