Background colour of UICellView Changing - objective-c

I have a table full of cells, and have changed the background colour by doing the following:
//set the backgorund color for the cells
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:0.5 green:0.3 blue:0.5 alpha:0.5];
//bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
cell.textLabel.backgroundColor = bg.backgroundColor;
The problem is that the background colour of the cells changes when you scroll them out of view, and then back into view.
Before:
After dragging the menu up, then back down:
I've tried to do some troubleshooting on my own and realized that this doesn't happen when I use one of the predefined UIColor , for example [UIColor redColor]

Take a look here.
Set the BackgroundColor property:
cell.backgroundColor = [UIColor greenColor];
in:
tableView:willDisplayCell:forRowAtIndexPath:

Related

Styling the unselectedItemTintColor UITabBar with iOS 15

I'm having trouble setting the tabbar.unselectedItemTintColor. There is some weird conflict when I try to set both the unselectedItemTintColor and the scrollEdgeAppearance.
I can run this code and things work as expected:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
tabBarController.delegate = self;
tabBar.translucent = NO;
UITabBar.appearance.shadowImage = [self colorForTabBar:[UIColor darkGrayColor]]; //this is the color of the seperator line at the top.
UITabBar.appearance.unselectedItemTintColor = [UIColor purpleColor];
UIColor *tintColor = [UIColor greenColor];
tabBar.barTintColor = tintColor;
tabBar.backgroundImage = [self colorForTabBar:tintColor];
The unselected items are purple, and the background is green. The problem is that if I scroll down to the bottom of my view controller then the scrollEdgeAppearance is shown instead of the default, and the green color goes transparent, and my shadowImage disappears. To combat this, I have tried setting:
if (#available(iOS 15.0, *)) {
tabBar.scrollEdgeAppearance = tabBar.standardAppearance;
}
And that makes everything turn gray! Even the standard default appearance turns gray.
I have played around with trying set all the appearance parameters like this:
if (#available(iOS 15.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = [UIColor greenColor];
appearance.shadowImage = [self colorForTabBar:[UIColor darkGrayColor]]; //this is the color of the seperator line at the top.
tabBar.tintColor = [UIColor redColor];
tabBar.unselectedItemTintColor = [UIColor purpleColor];
tabBar.standardAppearance = appearance;
tabBar.scrollEdgeAppearance = appearance;
}
And that almost works except the tabBar.unselectedItemTintColor isn't working, and all my icons are gray instead of purple.

Edit UITableViewCell property on scrolling

I am making an app in which there are quotes from famous people in an array which are being read out by openears and a tableView which shows all the quotes. The currently reading quote is to be displayed in the tableview in a different color than the other quotes. I tried with the following function in cellForRowAtIndexPath but the color of the cell remains the same unless the user scrolls it out and then come back to the view. But I need to set the color automatically and at the same time all other cells should revert back to the initial(white) color. Is there any method which can achieve this? I don't want to reload the table each time to achieve this.
if ([[arrayOfQuoteIds objectAtIndex:indexPath.row]intValue] == currentQuoteId) {
cell.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.contentView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.textLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.detailTextLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.accessoryView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
}
else {
cell.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
cell.accessoryView.backgroundColor = [UIColor whiteColor];
}
I can be wrong but how You want to achieve change of cell appearence without redrawing cell?
If You don't want reload all table try to reload just changed rows
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

UINavigationBar setTitleTextAttributes

I'm trying to change the color of NavigationBars title while I'm on a viewcontroller (not before pushing it). by using:
[[[self navigationController] navigationBar] setTitleTextAttributes:textAttributes];
But this line of code only works before pushes or pops. I was wondering if there is a way to force this without navigating?
I would say the simplest way is to create an UILabel with the same style you want for the UINavigationController title and set the label to the navigationItem.titleView.
Try this one
UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
create custom label and set it as titleView of your navigation bar

ios "Data Loading" Notification with transparent background

I am trying to create a view in iOS to let the user know their data is loading... it should have about a 200x200 Rounded-corner box in the middle with a spinner and the words "Data Loading..." and a transparent background.
This all is working except my 200x200 rounded-corner box is also transparent.
Here is the code I am using:
UIView *loadingDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 367)];
loadingDataView.alpha = 0.4;
loadingDataView.backgroundColor = [UIColor clearColor];
UIView *viewWithSpinner = [[UIView alloc] initWithFrame:CGRectMake(110, 106, 100, 100)];
[viewWithSpinner.layer setCornerRadius:15.0f];
viewWithSpinner.backgroundColor = [UIColor blackColor];
UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(5, 75, 90, 20)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 90, 70)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[spinner startAnimating];
msg.text = #"Data Loading";
msg.font = [UIFont systemFontOfSize:14];
msg.textAlignment = UITextAlignmentCenter;
msg.textColor = [UIColor whiteColor];
msg.backgroundColor = [UIColor clearColor];
viewWithSpinner.opaque = NO;
viewWithSpinner.backgroundColor = [UIColor blackColor];
[viewWithSpinner addSubview:spinner];
[viewWithSpinner addSubview:msg];
[loadingDataView addSubview:viewWithSpinner];
[self.view addSubview:loadingDataView];
Thanks.
No exactly an answer to your question, but check out the open source MBProgressHUD. It aims to be an open source replacement for the private UIProgressHUD class, and is exactly what you're trying to do.
The solution for your problem is that you should remove the alpha attribute of 0.4, that's what's turning your round view transparent, if your view is set to clearColor (this is not really a color, it just makes the view transparent) it makes no sense to add an alpha of 0.4. If what you want is a semi-transparent view surrounding your black rounded view, you should do the following:
[loadingDataView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 andAlpha:0.4]];
That will give you something whiteish kinda grayish, that should work.
However, I would recommend you to use the GIDAAlertView Class I developed, you can get the source and an example app on my GitHub:
It takes about 3 lines to get it working:
GIDAAlertView *spinnerAlert=[[GIDAAlertView alloc] initAlertWithSpinnerAndMessage:#"GIDAAlertView Spinner"];
//Show it ...
[spinnerAlert presentAlertWithSpinner];
//Later in your code, hide it
[spinnerAlert hideAlertWithSpinner];
This is how it looks like.
you are telling it to be clear...
loadingDataView.backgroundColor = [UIColor clearColor];
What do you want it to be black?

How can I programmatically set the background color of a UILabel?

I'm diving into iOS development and I'm programmatically creating some labels, but I can't seem to set their background color to black. From what I read, it seems simple enough, here's my code...
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height] autorelease];
[[lbl layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[lbl layer] setBorderWidth:1.0];
[[lbl layer] setBackgroundColor:[[UIColor blackColor] CGColor]];
[[self view] addSubview:lbl];
When I do this, the border color and width work as expected, but the background color remains white. Am I forgetting to do something?
Thanks so much for your help!
A UILabel already has a .backgroundColor property, you don't need to tweak its layer...
lbl.backgroundColor = [UIColor blackColor];
setting background color of UILabel
[lbl.backgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"color_lightblue.png"]]];
setting TextColor of UILabel with image color
[label_Description setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"color_lightgray.png"]]];
simple background color
lbl.backgroundColor = [UIColor blackColor];
swift 2
label.backgroundColor = UIColor.whiteColor()
other formula:
let label = UILabel(frame: CGRectMake(0, 0, view.frame.width, view.frame.height))
label.backgroundColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = message
label.numberOfLines = 2
label.font = UIFont.systemFontOfSize(12)
lbl.backgroundColor = [UIColor blackColor];
Use this
with one code you can set color
LabelObj.backgroundColor = [UIColor blackColor];