How to change UINavigationController background color? - ios

I'm able to change the backgroung image of UINavigationController by overriding drawRect: method:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed: #"navController.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = [UIColor blueColor];
}
#end
the background is what I intended to be and the tintColor as well, but when trying to set a color that isn't existing in UIColor class it fails and shows strange color:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed: #"navController.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = [UIColor colorWithRed:(26/255) green:(103/255) blue:(159/255) alpha:1];
}
#end
How can I force UINavigationBar to show the color I want?
Note: I'm only having a problem with navigation controller buttons color since the background itself is set to image.

You need to do this:
self.tintColor = [UIColor colorWithRed:(26.0f/255.0f) green:(103.0f/255.0f) blue:(159.0f/255.0f) alpha:1.0f];
Otherwise you're doing integer arithmetic and you'll end up with 0 for all of them probably. Use floating point arithmetic and you get the values you desire.

This Works for me
self.navigationController.navigationBar.backgroundColor= [UIColor colorWithRed:57.0/255.0 green:158.0/255 blue:209.0/255 alpha:1.0];

You can also use:
navigationController.view.backgroundColor = .yourColor

Related

IOS/Objective-C: Change tint of an image view

I would like to change the color of an image view when the user touches it. I don't need it to change a great deal. However, I would like it to change to a shade of blue. I found the following code to change the gradient but it is not having any effect. I don't need a gradient, actually, only a tint. Would appreciate any suggestions.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor];
[snapshot.layer insertSublayer:gradient atIndex:0];
An easy and non-intrusive way is to add a translucent overlay to your image view. This can then be shown and hidden as the button is pressed and released.
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.bounds];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here
[imageView addSubview:overlay];
}
overlay.hidden = !on;
}
You can try giving the UIImage a tint
UIImage *image = [[UIImage imageNamed:#"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work
imageView.image = newImage;
And then changing that tint when the user touches the image using a tap gesture recognizer

Completely transparent UITabBar in iOS 8

I'm trying to make my tabBar transparent, I've searched but all I found was articles resulting in partly and not fully transparent tabBars and some were for IOS 5, etc.
I would like to accomplish this as seen in Sketch 3:
What's the easiest way to accomplish this?
I thought of doing this:
// Make the tabBar transparent
self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
self.tabBarController.tabBar.translucent = YES;
but that result wasn't exactly perfect:
Really appreciate help!:)
Sincerely,
Erik
Update
// Make the tabBar transparent
[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
self.tabBarController.tabBar.translucent = YES;
Have you tried the barTintColor?
[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage new]];
That should do the trick.
Swift 3.0
... call this code in the AppDelegate's didFinishLaunchingWithOptions
let tabBar = UITabBar.appearance()
tabBar.barTintColor = UIColor.clear
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage()
The result will be a transparent background for every UITabBar.
You need to subclass the UITabBarController and in the viewdidload: you should put this code:
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.tabBar setBackgroundImage:transparentImage];
[self.tabBar setShadowImage:transparentImage];
// self.tabBar.alpha = 0.0;
I solved the problem using the following code:
let tabBarController = UITabBarController()
...
tabBarController.tabBar.backgroundImage = UIImage()
tabBarController.tabBar.barTintColor = .clear
tabBarController.tabBar.isTranslucent = true
tabBarController.extendedLayoutIncludesOpaqueBars = true
tabBarController.edgesForExtendedLayout = .all

iOS 7 navigation bar height

Is it possible to change navigation bar height in iOS 7?
why I need navigation bar:
to have a transparent navigation bar with the right positions of others elements on my view without any problems;
I don't want to create a fake "navigation bar" - in this case I'll need to set up all positions myself.
//make real nav bar invisible
self.navigationBar.barTintColor = [UIColor clearColor];
UIImage* transparentImage = [UIImage emptyImageWithSize:CGSizeMake(self.navigationBar.frame.size.width, 1) andBackgroundColor:[UIColor clearColor]];
[self.navigationBar setBackgroundImage:transparentImage forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = transparentImage;
and then
UINavigationBar* fakeNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, neededHeight)];
fakeNavigationBar.barTintColor = [UIColor whiteColor];
[self.navigationBar insertSubview:fakeNavigationBar atIndex:0];
where
+ (UIImage*)emptyImageWithSize:(CGSize)size andBackgroundColor:(UIColor*)color
{
CGRect frameRect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, color.CGColor); //image frame color
CGContextFillRect(ctx, frameRect);
UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
In your app delegate
UINavigationController *NavController=[[UINavigationController alloc]initWithRootViewController:HomeViewController];
[NavController.navigationBar setBounds:CGRectMake(10, 30, 40, 10)];

Remove gloss/gradient effect from Navigation/Tab bars

I would like to remove the gradient effect that occurs in the UINavigationBar and UITabBar. The following picture shows an example tab bar using the custom UIColor of 7/29/88 (RGB), set using setTintColor:color and as you can see, the tab bar has a gloss in the top half of the bar.
How do I remove this?
Depends on your definition of "remove". In iOS 6.x (didn't test iOS 4/5) the following works.
// this will show a tab bar with a solid background color
tabBar.backgroundImage = [UIImage new];
tabBar.backroundColor = [UIColor blueColor];
// this will show a navigation bar with a solid background color
[navBar setBakgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault]];
navBar.shadowImage = [UIImage new];
navBar.backgroundColor = [UIColor blueColor];
navBar.tintColor = [UIColor blueColor];
It's not possible. However you can use custom background images. Check UIAppearance documentation
I remove the gradient effect from my Navigation Bar, you can try this code and see if its works for you too.
//First, create your own Navigation Bar Class, and add this to your init method.
self.tintColor = [UIColor clearColor];
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage"]];
//Add this to your DrawRect method
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage"]];
//If you want a plain color change this
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents([color CGColor]));
CGContextFillRect(context, rect);
}

iOS - [UIColor clearColor] and UIToolbars

I've been attempting to use [UIColor clearColor] with UIToolbar in an attempt to make a custom control interface more fitting of a "Mechanical" application (Think buttons you would see in a Movie from the 70s).
What is happening is that when I set the toolbar to clearColor it is turning it matte black. The image behind it is red, tan and black so I'm sure it's not working as intended.
One difference I see is that I'm using the toolbar on a nav controller and not a stand alone UIToolbar.
The lines of code are
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];
and my upper navigation bar (that is setup in another view) is UIBarStyleBlackTranslucent, could this be throwing it off?
any help tracking this down would be great.
You can set a transparent background for the toolbar of your navigation controller with the following code:
// UIToolbar.h
#interface UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect;
#end
// UIToolbar.m
#import "TransparentToolbar.h"
#implementation UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect {
[[UIColor clearColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
#end
Usage:
// bar_bottom_bumped.png is a toolbar image with transparency
UIImage *bg = [UIImage imageNamed:#"bar_bottom_bumped.png"];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = self.navigationController.toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];
[self.navigationController.toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];

Resources