Add transparent overlay as subview of `uiimageview` - ios

I have a UITable with custom cells, each of which has a uiimage. When I highlight the cell, the image is not getting highlighted.
To highlight the cell's image, when I select a cell in my table, didHighlightRowAtIndexPath is called. In that method, I change the background color of the cell. I also call a method, indicatedImageViewPressed, which I got from this post, to add an overlay to the image of the cell.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
SpeciesCell* speciesCell = (SpeciesCell*)[tableView cellForRowAtIndexPath:indexPath];
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
[self indicatedImageViewPressed:speciesCell.imageView on:YES];
}
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.frame];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000]; // Your color overlay goes here
[imageView addSubview:overlay];
[imageView bringSubviewToFront:overlay];
}
overlay.hidden = !on;
}
However, the cell's image does not change. No overlay is added. I tried bringSubviewToFront to make sure the overlay wasn't obscured by some other view, but it still doesn't work.
UPDATE: I am able to add a border width/color to speciesCell.speciesImage

speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
replace this with below code:
SpeciesCell.selectionStyle=UITableViewCellSelectionStyleGray;

Related

How to implement a drop shadow with rounded corner in a UITableViewCell?

I want to implement the drop shadow and rounded corner in UITableViewCell.
- (void)tableView:(UITableView *)tableView willDisplayCell:(MembershipCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor clearColor];
CGRect rect = cell.frame;
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(8,8,rect.size.width-8,rect.size.height)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(0,0);
whiteRoundedCornerView.layer.shadowOpacity = 0.75;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
I am using the above code but it shows the shadow in the top and left side of the cell.
That is because you dont have space on right and bottom, you need to have some space at right and bottom as well for shadow to show up. Also, you can add shadowRadius to the layer for controlling the radius of the shadow. Try following
- (void)tableView:(UITableView *)tableView willDisplayCell:(MembershipCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor clearColor];
CGRect rect = cell.frame;
UIView *whiteRoundedCornerView;
if (![cell.contentView viewWithTag:SOME_TAG_VALUE]) {
whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(8,8,rect.size.width-16,rect.size.height-16)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(0,0);
whiteRoundedCornerView.layer.shadowOpacity = 0.75;
whiteRoundedCornerView.layer.shadowRadius = 1.0;
whiteRoundedCornerView.tag = SOME_TAG_VALUE;
[cell.contentView addSubview:whiteRoundedCornerView];
}
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}

Change UILabel textColor when UITableViewCell is highlighted?

I have a custom UITableViewCell, with an UILabel and an UIImageView. I want to change the background color and the text color when the cell is highlighted. In my CustomCell's setHighlighted method, I have the following piece of code:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(self) {
if(highlighted) {
self.title.textColor = [UIColor whiteColor];
} else {
self.title.textColor = [UIColor blackColor];
}
//highlight background
UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
bgColorView.backgroundColor = [UIColor blackColor];
[self setSelectedBackgroundView:bgColorView];
}
}
I already tried to put the code for textColor change in the tableView's didSelectRowAtIndexPath, but it's not the effect that I want - I want the text color to change when the user touches down on the cell - not at touch up.
Any suggestions?
You should use the attribute highlightedTextColor. Set the color for the cell inside tableView:cellForRowAtIndexPath and it should look like this:
cell.textLabel.highlightedTextColor = [UIColor blueColor];
try this
[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
If you have custom label then use following code
lblPrd.highlightedTextColor = [UIColor whiteColor];
override method
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
//TODO set view highlighted color
}
in custom tableviewcell
In case you have a Storyboard or XIB, you can simply set this at Attributes Inspector > Label > Highlighted.

Footer view's color always darker than UITableView separator's color

In my UITableView, I set the separator color like this:
- (void)viewDidLoad {
...
self.tableView.separatorColor = [UIColor lightGrayColor];
...
}
And I set the color of the footer like this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc]
initWithFrame:(CGRect){0, 0, 320, 1}];
footerView.contentView.backgroundColor = [UIColor lightGrayColor];
return footerView;
}
However, the footer view's color is always darker than the separator's color, like this:
How do I get them to be the exact same color? Thanks.
From iOS 6.0 onwards, you can use the below mentioned UITableView's delegate method to change the color of the footer view's background-
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
//Set the background color of the View
view.tintColor = [UIColor blueColor];
}
None of the above worked for me. The only thing that worked was the solution from this thread:
White Separator Above Table Section Headers
Basically you need to manually add in the footer separator 1 px above your custom footer view.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UITableViewHeaderFooterView *versionView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, maxScreenWidth, 50)];
UIView *footerSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, -1, maxScreenWidth, 1)];
versionView.backgroundColor = [UIColor blackColor];
footerSeparator.backgroundColor = [UIColor darkGrayColor];//Your color
[versionView addSubview:footerSeparator];
return versionView;
}
The only problem I have with this is that the footer separator seems to disappear if you scroll away from it and then back to it in the table view.

UITableView extend different background color below cells

I have a UITableView that I have given a headerView that has a green background. I have also given the UITableView that same green background color so that when the tableView is pulled down it feels like a seemless color above the tableView. Works perfectly.
The problem is my tableView only has 4 rows, and below that it shows the green color again.
How can I show white below the rows instead so that I only see the green background color when pulling down the tableView?
You can add an extra top view, same as in this answer to UIRefreshControl Background Color:
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.backgroundColor = [UIColor greenColor];
[self.tableView insertSubview:bgView atIndex:0];
You could add a fifth cell with no content, and make it, say 400 points high, and limit the size of the table's contentView so that you can't scroll to the bottom of that cell.
-(void)viewWillLayoutSubviews {
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, 500);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat h = (indexPath.row == 4)? 400:44;
return h;
}
Putting the contentSize setting in viewWillLayoutSubviews ensures that it will be reset to that value if you rotate your device.
as I said in these comments, in this way the headerView will be under the navigationBar and if you pull down the tableview you will see the headerView green. So you can set white color for tableView:
(i tested it and work)
-(void) viewDidLoad {
self.tableView.backgroundColor = [UIColor whiteColor];
headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor greenColor];
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, 320, 120)];
lab.backgroundColor = [UIColor greenColor];
[headerView addSubview:lab];
self.tableView.tableHeaderView = headerView;
}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 80;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
Same as the accepted answer - in Swift 5:
var frame : CGRect = self.tableView.bounds
frame.origin.y = -frame.size.height
let bgView : UIView = UIView.init(frame: frame)
bgView.backgroundColor = UIColor.green
self.tableView.insertSubview(bgView, at: 0)
Also add the following code (right after the above code) to make sure it works for other sized devices:
bgView.translatesAutoresizingMaskIntoConstraints = true
bgView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth]

UITableView separator line does not show in iPhone app

I have app in which i am using tableView problem is that when tableView has one record then it does not show separator line but shows when there are two records.
The first cell in tableView is textField. here is the code i am using.
for (UIView *subView in cell.subviews)
{
if (subView.tag == 2 || subView.tag == 22)
{
[subView removeFromSuperview];
}
}
tableView.backgroundView=nil;
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.separatorInset = UIEdgeInsetsZero;
if(indexPath.section==0){
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,0,248,31)];
tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
tagInputField.textAlignment=UITextAlignmentLeft;
tagInputField.backgroundColor=[UIColor whiteColor];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
[tagInputField.layer setCornerRadius:5.0f];
[tagInputField.layer setMasksToBounds:YES];
tagInputField.layer.borderWidth = 0.3;
tagInputField.layer.borderColor = [UIColor darkGrayColor].CGColor;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#"Enter tag here "];
tagInputField.textColor =[UIColor grayColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[cell addSubview:tagInputField];
return cell;
}
if(indexPath.section==1) {
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
crossButton.tag = 22; //use a tag value that is not used for any other subview
//crossButton.backgroundColor = [UIColor purpleColor];
crossButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Cross.png"]];
[cell addSubview:crossButton];
cell.textLabel.font =[UIFont fontWithName:#"Myriad-Pro" size:8];
cell.textLabel.textColor =[UIColor grayColor];
cell.backgroundColor=[UIColor whiteColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
[tagInputField setFrame:CGRectMake(5,0,248,31)];
tableView.backgroundColor=[UIColor whiteColor];
[tagInputField.layer setCornerRadius:0.0f];
[tagInputField.layer setMasksToBounds:YES];
tagInputField.layer.borderWidth = 0.0;
tagInputField.layer.borderColor = [UIColor clearColor].CGColor;
return cell;
}
When you will have only 1 record then separator will not show, and you should setting the separator for tableView at viewDidLoad look like
- (void)viewDidLoad
{
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
and in any case you want to show your own separator for every single cell ten try to add a imageView or somrthing witch share by the table cell
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
separatorView.layer.borderColor = [UIColor redColor].CGColor;
separatorView.layer.borderWidth = 1.0;
[cell.contentView addSubview:separatorView];
How to customize tableView separator in iPhone
go for more on it....
Just try like this in side the Cell_for_Row_At_Index_path delegate method:
[tableView setSeparatorInset:UIEdgeInsetsZero];
[tableView setSeparatorColor:[UIColor greenColor]];
just paste this line and check.
add an empty footer at the end of the tableview to show the separator. To do so implement the following tableview delegate method in your class with your other tableview delegate functions:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.001;
}
I used this to add last cell separator line....
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
separatorLine.layer.borderColor = [UIColor grayColor].CGColor;
separatorLine.layer.borderWidth = 1.0;
return separatorLine;
}
I found none of the above answers worked. I was particularly wary of the answer suggesting adding a UIView to look like the line.
In the end I found that this answer worked for me:
tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
In another answer it was suggested that adding worked better
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
but I found that this was unnecessary.
All the credit goes to user859045 and samvermette for their answers on different threads. samvermette's thread especially has a lot of answers on this topic which are very helpful and worth a look.

Resources