Styling the unselectedItemTintColor UITabBar with iOS 15 - objective-c

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.

Related

sendSubviewToBack on UITableView not working properly in iOS 11

I have an UITableViewController to which I successfully applied in the past a gradient background, by sending the newly added subview to back:
//performed on viewDidLoad
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.5*280, 1.5*SCREEN_HEIGHT)];
bgView.backgroundColor = [UIColor yellowColor];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = bgView.bounds;
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 1);
UIColor *topColor = UIColorFromRGB(0x229f80);
UIColor *bottomColor = UIColorFromRGB(0x621ad9);
gradient.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
[bgView.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:bgView];
[self.view sendSubviewToBack:bgView];
bgView = nil;
However, this no longer works in iOS 11 and the bgView is actually placed on top of all the cells.
Anyone knows how I can fix this?
Or maybe I was doing it wrong all the time?
If your cells are transparent then you can try self.tableView.backgroundView = bgView;
If you don't need the background view to be scrolling together with the table view, you can use
self.tableView.backgroundView = bgView;
If you need the background view to be scrolling, change layer's zPosition to a negative value to make it work in iOS 11:
[self.view insertSubview:bgView atIndex:0];
bgView.userInteractionEnabled = NO;
bgView.layer.zPosition = -1;
Another way to fix it is to call [self.view sendSubviewToBack:bgView]; in tableView:willDisplayCell:forRowAtIndexPath:
It works for not transparent cells also.
it appears that addSubview(UIView) sendSubview(toBack: UIView) no longer works for UITableViewControllers in iOS11. So I swap to this:
// in ViewDidLoad
// set the backgroundImage
let backgroundImage = UIImageView(frame: self.view.bounds)
backgroundImage.image = UIImage(named: "background.png")
// self.view.addSubview(backgroundImage) // NO LONGER WORKS
// self.view.sendSubview(toBack: backgroundImage) // NO LONGER WORKS
self.tableView.backgroundView = backgroundImage

iOS8 UIButton title disappear

I have a button in my app that is perfectly visible on iOS7. But when I switch to iOS8 the title label for this button does not appear. And I can see only border around the button.
Here is the code that makes the button. Could someone give me a clue where the problem is?
UIButton *problem = [[UIButton alloc] initWithFrame:frame];
[problem addTarget:self action:#selector(problemButton:) forControlEvents:UIControlEventTouchUpInside];
problem.backgroundColor = [UIColor whiteColor];
[problem setTitle:NSLocalizedString(#"ReportAProblem", nil) forState:UIControlStateNormal];
problem.titleLabel.textColor = CUSTOM_DARK_GRAY_COLOR;
[[problem layer] setBorderWidth:1.0f];
[[problem layer] setBorderColor:CUSTOM_DARK_GRAY_COLOR.CGColor];
problem.layer.cornerRadius = 5;
problem.clipsToBounds = YES;
I found an answer for this question but immediately there is another:
I encounter the very same problem with some of my UILabels, for example:
self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.numberOfLines = 1;
self.label.adjustsFontSizeToFitWidth = YES;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor = self.textColor;
CGFloat fontSize = self.bounds.size.height / 5;
self.label.font = [UIFont systemFontOfSize:fontSize];
You should use - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state instead of titleLabel.textColor.
Second problem: Why are you trying to set a ratio of your view height as a font size ? Maybe doing it with your label makes more sense. CGFloat fontSize = self.label.bounds.size.height / 5; Anyway what you usually want to do is have a fixed font size and adapt the label frame around it.

Background colour of UICellView Changing

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:

Changing elements in selected UITableViewCell subclass

I have implemented a custom UITableViewCell for my application and have created a custom background view and selectedbackgroundview, both of which work great. However, I have several other images and uilabels on the cell that I want to change the color's of when it is selected. I have overwritten the following method and it logs correctly when the cell is selected but it does not change the image or the text color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
NSLog(#"setSelected:YES");
separatorImage.image = [UIImage imageNamed:#"SeparatorSelected"];
titleLabel.textColor = [UIColor whiteColor];
} else {
NSLog(#"setSelected:NO");
separatorImage.image = [UIImage imageNamed:#"Separator"];
titleLabel.textColor = [UIColor grayColor];
}
}
Any Idea what I am doing wrong?
Update
separatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Separator"]]];
[separatorImage setFrame:CGRectMake(99, 4, 5, 71)];
separatorImage.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:separatorImage];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(111, 4, 188, 26)];
titleLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:21.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:titleLabel];
I have a hunch that sending -setSelected:animated: to super at the beginning of the method is preventing the changes to your cell. Try moving the call to super to the end of the method. You should also override -setHighlighted:animated: or your selection will appear to lag.

How make a transparent & flat UIBarButtonItem?

I wanna to make buttons for my app flat & transaparent, like the ones in the kindle app (see the shop button).
I try to set a custom background by code with not luck (the button is draw as a rectangle, with he border color fill all the background):
- (UIImage *) screenshot {
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = self.bounds.size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+(UIImage *) buttonFlat {
UIView *bt = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)];
bt.backgroundColor = [UIColor blackColor];
bt.layer.borderColor = [UIColor lightGrayColor].CGColor;
bt.layer.borderWidth = 1;
bt.layer.cornerRadius = 5.0;
bt.alpha = 0.7;
return [bt screenshot];
}
I know how do this with a normal UIButton, but prefer to make this so I can retain the standard icons of the UIBarButtonItem if possible...
A UIBarButton can hold any custom view:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:someView];