UISearchBar set translucent to NO doesn't work - ios

(I know this may seem as duplicate, but I'm telling you all nothing I've found here worked)
I have a UITableView with a search display controller and a search bar int it, I'm trying to set the tintColor according to the company colors. But it's translucent no matter what I try.
Here is the sample code:
[self.searchDisplayController.searchBar setTranslucent:NO];
[self.searchDisplayController.searchBar setBarTintColor:[[ConfToolbox sharedInstance] getBarTintColor]];
[[ConfToolbox sharedInstance] getBarTintColor] returns a dark-ish blue UIColor .
I've looked around for an answer but nothing worked, even this accepted answer.
Any help would be great.
Cheers,
Ayu.

I achieved what I wanted using this, just in case someone faces the same problem:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,10), YES, 0);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[ConfToolbox sharedInstance] barTintColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0,0,10,10));
UIImage* coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.searchDisplayController.searchBar setBackgroundImage:coloredImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
You can set the second parameter of CGContextSetFillColorWithColor with whatever color you want.
From the documentation
The default value is YES. If the search bar has a custom background
image, the default is YES if any pixel of the image has an alpha value
of less than 1.0, and NO otherwise.
Since the image is made from a color with an alpha value of 1, the translucent effect is off as stated.

try like this,
self.searchBar.frame = CGRectMake(self.searchBar.frame.origin.x, self.searchBar.frame.origin.y+7, self.searchBar.frame.size.width, 30);
self.searchBar.placeholder = #"Search...";
self.searchBar.layer.cornerRadius = 15;
self.searchBar.layer.backgroundColor = [UIColor redColor].CGColor;
self.searchBar.layer.borderWidth=8;
[[NSClassFromString(#"UISearchBarTextField") appearanceWhenContainedIn:[UISearchBar class], nil] setBorderStyle:UITextBorderStyleNone];
self.searchBar.layer.borderColor=[UIColor redColor].CGColor;

Related

Achieving bright, vivid colors for an iOS 7 translucent UINavigationBar

iOS 7.1 UPDATE: Looks like the workaround for modifying the alpha channel in the UINavigationBar has been ignored in this update. Right now, the best solution seems to be to just 'deal with it' and hope that whatever color you choose can render a translucent effect. I am still looking into ways of getting around this.
iOS 7.0.3 UPDATE: The GitHub library we created has been updated to slightly work around this issue when using iOS 7.0.3. Unfortunately, there is no magic formula to support both colors created in iOS 7.0.2 and earlier and iOS 7.0.3. Seems like Apple improved the saturation, but at the cost of opacity (since the blurred translucency is dependant on the opacity level). I, along with a few others, are working on creating a much better fix for this.
I'm sure many people have already come across the problem where iOS 7 tends to desaturate the color of a UINavigationBar that is translucent.
My goal is to achieve a UINavigationBar with this tint color, but translucent:
However, with translucency, I'm getting this. The background view is white, which I understand will make this view a bit lighter:
Is there any way to achieve the original color while still having translucency? I've noticed Facebook has been able to get their bar to be their rich, blue color, as displayed here:
..so I know there has to be some way. Background views obviously make a difference here, but most of their content is also gray/white. It seems that regardless of whatever bar tint color you put in, you are unable to get vivid colors under translucency.
Updated with solution.
Here's the solution that I ended up coming up with. I took aprato's solution and then encompassed the custom UINavigationBar within a UINavigationController subclass. I have created a repository that has this implementation listed below, along with an example app.
////////////////////////////
// CRNavigationBar.m
////////////////////////////
#import "CRNavigationBar.h"
#interface CRNavigationBar ()
#property (nonatomic, strong) CALayer *colorLayer;
#end
#implementation CRNavigationBar
static CGFloat const kDefaultColorLayerOpacity = 0.5f;
static CGFloat const kSpaceToCoverStatusBars = 20.0f;
- (void)setBarTintColor:(UIColor *)barTintColor {
[super setBarTintColor:barTintColor];
if (self.colorLayer == nil) {
self.colorLayer = [CALayer layer];
self.colorLayer.opacity = kDefaultColorLayerOpacity;
[self.layer addSublayer:self.colorLayer];
}
self.colorLayer.backgroundColor = barTintColor.CGColor;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.colorLayer != nil) {
self.colorLayer.frame = CGRectMake(0, 0 - kSpaceToCoverStatusBars, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kSpaceToCoverStatusBars);
[self.layer insertSublayer:self.colorLayer atIndex:1];
}
}
#end
////////////////////////////
// CRNavigationController.m
////////////////////////////
#import "CRNavigationController.h"
#import "CRNavigationBar.h"
#interface CRNavigationController ()
#end
#implementation CRNavigationController
- (id)init {
self = [super initWithNavigationBarClass:[CRNavigationBar class] toolbarClass:nil];
if(self) {
// Custom initialization here, if needed.
}
return self;
}
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithNavigationBarClass:[CRNavigationBar class] toolbarClass:nil];
if(self) {
self.viewControllers = #[rootViewController];
}
return self;
}
#end
iOS 7.0.3 UPDATE: As you see above 7.0.3 changed things. I've updated my gist. Hopefully this will just go away as people upgrade.
Original Answer:
I ended up with a hack combining the two of the other answers. I'm subclassing UINavigationBar and adding a layer to the back with some extra space to cover if any of the various height status bars are up. The layer gets adjusted in layout subviews and the color changes whenever you set barTintColor.
Gist: https://gist.github.com/aprato/6631390
setBarTintColor
[super setBarTintColor:barTintColor];
if (self.extraColorLayer == nil) {
self.extraColorLayer = [CALayer layer];
self.extraColorLayer.opacity = self.extraColorLayerOpacity;
[self.layer addSublayer:self.extraColorLayer];
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;
layoutSubviews
[super layoutSubviews];
if (self.extraColorLayer != nil) {
[self.extraColorLayer removeFromSuperlayer];
self.extraColorLayer.opacity = self.extraColorLayerOpacity;
[self.layer insertSublayer:self.extraColorLayer atIndex:1];
CGFloat spaceAboveBar = self.frame.origin.y;
self.extraColorLayer.frame = CGRectMake(0, 0 - spaceAboveBar, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + spaceAboveBar);
}
The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. To tint the bar's background, please use -barTintColor.You can use following code to make the app work with both ios6 and ios7.
if(IS_IOS7)
{
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = NO;
}
else
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
IS_IOS7 is a macro which is defined in pch file as follows.
#define IS_IOS7 ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
I didn't come up with this solution but it seems to work fairly well. I just added it to viewDidLoad on my subclass of UINavigationController.
Source: https://gist.github.com/alanzeino/6619253
// cheers to #stroughtonsmith for helping out with this one
UIColor *barColour = [UIColor colorWithRed:0.13f green:0.14f blue:0.15f alpha:1.00f];
UIView *colourView = [[UIView alloc] initWithFrame:CGRectMake(0.f, -20.f, 320.f, 64.f)];
colourView.opaque = NO;
colourView.alpha = .7f;
colourView.backgroundColor = barColour;
self.navigationBar.barTintColor = barColour;
[self.navigationBar.layer insertSublayer:colourView.layer atIndex:1];
One low-fi way would probably be pinning a UIView that is the height of the Navigation Bar to the top of the view behind the bar. Make that view the same color as the navigation bar but play with the alpha until you get the desired effects:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.navigationController.navigationBar.frame), 64)];
backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1 alpha:.5];
[self.navigationController.view insertSubview:backgroundView belowSubview:self.navigationController.navigationBar];
UIView behind
(Changed color from lower examples to emphasis transparency. Transparency/blurring is more noticeable when in movement.)
Subclassing the UINavigationBar and placing that same view above the background but behind everything else will probably achieve similar results while being less hacky.
Another solution I've seen tossed around is playing with the alpha of the UINavigationBar:
self.navigationController.navigationBar.alpha = 0.5f;
Edit: Actually, after testing it seems like this doesn't provide the intend behavior (or any behavior):
.8 alpha
Unadjusted alpha
Obviously, you will only want to do this on iOS 7 devices. So, add some version check before you implement any of these.
Instead of creating your UIColor object in the RGB format, use HSB and increase the saturation parameter. (Credits to Sam Soffes who describes this method here)
navigationBar.barTintColor = [UIColor colorWithHue:0.555f saturation:1.f brightness:0.855f alpha:1.f];
Note: This solution is a tradeoff and doesn't work well for colors with high saturation.
To pick the HSB color from your design you can use a tool like ColorSnapper which allows you to simply copy the UIColor HSB format.
You can also try the UIColor Category (GitHub Link) from David Keegan to modify existing colors.
The problem has now been fixed by Apple in the new 7.0.3 release.
I used #aprato's solution but found a few corner cases where the new layers from new VCs (eg. UINavigationItemButtonViews, UINavigationItemViews, etc) would be automatically inserted into a position below the extraColorLayer (which would cause those title or button elements to be affected by the extraColorLayer and thus fainter in color than they normally would be). So I adjusted #aprato's solution to force the extraColorLayer to stay at the index position 1. At index position 1, the extraColorLayer stays right above the _UINavigationBarBackground, but underneath everything else.
Here's my class implementation:
- (void)setBarTintColor:(UIColor *)barTintColor
{
[super setBarTintColor:barTintColor];
if (self.extraColorLayer == nil)
{
self.extraColorLayer = [CALayer layer];
self.extraColorLayer.opacity = kDefaultColorLayerOpacity;
[self.layer insertSublayer:self.extraColorLayer atIndex:1]; // This way the text comes out clear
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.extraColorLayer != nil)
{
self.extraColorLayer.frame = CGRectMake(0, 0 - kSpaceToCoverStatusBars, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kSpaceToCoverStatusBars);
}
}
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
{
[super insertSubview:view aboveSubview:siblingSubview];
[self.extraColorLayer removeFromSuperlayer];
[self.layer insertSublayer:self.extraColorLayer atIndex:1]; // This way the text comes out clear
}
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
[super insertSubview:view atIndex:index];
[self.extraColorLayer removeFromSuperlayer];
[self.layer insertSublayer:self.extraColorLayer atIndex:1]; // This way the text comes out clear
}
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
{
[super insertSubview:view belowSubview:siblingSubview];
[self.extraColorLayer removeFromSuperlayer];
[self.layer insertSublayer:self.extraColorLayer atIndex:1]; // This way the text comes out clear
}
I've improved your code in my fork: https://github.com/allenhsu/CRNavigationController
With my modification, the result color on screen (picked on white background) will be exactly the same value passed into setBarTintColor. I think it's an amazing solution.
None of these hacks are required :). Simply set:
self.navigationController.navigationBar.translucent = NO;
For iOS 7, the default translucency has been kept to TRUE.
On a related note, you can set your title text color (with shadow) easily via:
NSShadow *titleShadow = [[NSShadow alloc] init];
titleShadow.shadowOffset = CGSizeMake(0.0f, -1.0f);
titleShadow.shadowColor = [UIColor blackColor];
NSDictionary *navbarTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor],
NSShadowAttributeName: titleShadow};
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
I came across this Q/A while trying to setup an uniformly colored navigation bar with transparency DISABLED on iOS 7.
After experimenting a while with barTintColor I figured out that a very easy way of having an opaque navigation bar is to make a single pixel image of the desired color, make a stretchable image out of it, and setting it to the backgroundImage of the navigation bar.
UIImage *singlePixelImage = [UIImage imageNamed:#"singlePixel.png"];
UIImage *resizableImage = [singlePixelImage resizableImageWithCapInsets:UIEdgeInsetsZero];
[navigationBar setBackgroundImage:resizableImage forBarMetrics:UIBarMetricsDefault];
Three lines of code, very simple and works BOTH on iOS 6 and iOS 7 (barTintColor is unsupported on iOS 6).
Theres a great Dropin UINavigationController replacement available from Simon Booth available at GitHub Here GitHub - C360NavigationBar
If you're backward supporting iOS6 do a check on the root view controller as such:
PatientListTableViewController *frontViewController = [[PatientListTableViewController alloc] init];
UINavigationController *navViewController = [[UINavigationController alloc] initWithNavigationBarClass:[C360NavigationBar class] toolbarClass:nil];
if ([navViewController.view respondsToSelector:#selector(setTintColor:)]) {
//iOS7
[navViewController.view setTintColor:self.navBarTintColor];
[[C360NavigationBar appearance] setItemTintColor:self.navBarItemTintColor];
} else {
//iOS6
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
navViewController.navigationBar.tintColor = self.navBarTintColor;
}
[navViewController pushViewController:frontViewController animated:NO];
self.window.rootViewController = navViewController;
As #bernhard mentioned above it's possible to saturate the bar tint color to get desired navigation bar appearance.
I wrote an BarTintColorOptimizer utility for that kind of adjustment. It optimizes translucent bar tint color to make the bar's actual color match the desired color in iOS 7.x and later. Look at this answer for details.
Frankly speaking, above answers might be right but following trick worked for me with very ease.
// this is complete 100% transparent image
self.imageBlack = [[UIImage imageNamed:#"0102_BlackNavBG"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)
resizingMode:UIImageResizingModeStretch];
// this is non-transparent but iOS7
// will by default make it transparent (if translucent is set to YES)
self.imageRed = [[UIImage imageNamed:#"0102_RedNavBG"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)
resizingMode:UIImageResizingModeStretch];
// some navigation controller
[nvCtrLeft.navigationBar setBackgroundImage:self.imageRed
forBarMetrics:UIBarMetricsDefault];
// some another navigation controller
[nvCtrCenter.navigationBar setBackgroundImage:self.imageRed
forBarMetrics:UIBarMetricsDefault];
Here are the images used for self.imageRed and self.imageBlack.
< > black image is in this brackets won't be visible as it is transparent :)
< > red image is in this brackets.
is there a way to use #aprato solution without subclassing UINavigationBar.
In my project my main view is a UIViewController.
the problem is that the navigationController is a readonly property, is there a way to use you class with my project because i can't use : [[UINavigationController alloc] initWithNavigationBarClass:
thanks
An easy way to get the color you want is using
[<NAVIGATION_BAR> setBackgroundImage:<UIIMAGE> forBarPosition:<UIBARPOSITION> barMetrics:<UIBARMETRICS>];
As long as your image has some alpha, the translucency will work and you can set the alpha by changing the image. This was just added in iOS7. The width and height for the image are 640x88px for vertical (add 20 to the 88 if you want it to be underneath the status bar).

Get the right color in iOS7 translucent navigation bar

How can I get the right coloring for my translucent navigation bars in iOS 7? The navigation bar just adjusts the given color to a much brighter one. Changing brightness or saturation of the color also doesn´t deliver the right result.
Anyone having the same trouble? It seems to work somehow, looking at Facebook: they´re having their colors and translucent navigation bars.
Edit: Just to make it clear: I need the Bar to be translucent, not transparent (with some alpha), not solid! http://en.wikipedia.org/wiki/Transparency_and_translucency
Edit: Now posted to Apple BugReporter
The bar will adjust your color values.
Preferred method, for RGB >= 40 only, will give the most blurring
You can use this calculator and put in what you want the color to be when rendered on screen, it will tell you what to set the color of the barTintColor so when Apple adjusts it, it will show as intended
https://www.transpire.com/insights/blog/bar-color-calculator/
Edit: Note that these calculations are for a white background, and for lighter colours (rgb over 40, if you need darker, you will need to add a background layer like others have mentioned - although that will reduce the bar's blur)
In depth guide: https://www.transpire.com/insights/blog/custom-ui-navigationbar-colors-ios7/
Snippet:
#interface UnderlayNavigationBar : UINavigationBar
#end
.
#interface UnderlayNavigationBar ()
{
UIView* _underlayView;
}
- (UIView*) underlayView;
#end
#implementation UnderlayNavigationBar
- (void) didAddSubview:(UIView *)subview
{
[super didAddSubview:subview];
if(subview != _underlayView)
{
UIView* underlayView = self.underlayView;
[underlayView removeFromSuperview];
[self insertSubview:underlayView atIndex:1];
}
}
- (UIView*) underlayView
{
if(_underlayView == nil)
{
const CGFloat statusBarHeight = 20; // Make this dynamic in your own code...
const CGSize selfSize = self.frame.size;
_underlayView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, selfSize.width, selfSize.height + statusBarHeight)];
[_underlayView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[_underlayView setBackgroundColor:[UIColor colorWithRed:0.0f green:0.34f blue:0.62f alpha:1.0f]];
[_underlayView setAlpha:0.36f];
[_underlayView setUserInteractionEnabled:NO];
}
return _underlayView;
}
#end
.
UIViewController* rootViewController = ...;
UINavigationController* navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[UnderlayNavigationBar class] toolbarClass:nil];
[navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.0f green:0.0f blue:90.0f/255.0f alpha:1]];
[navigationController setViewControllers:#[rootViewController]];
You simply need to change translucent property
navigationBar.translucent = NO;
It is effectively the same as removing/making transparent subviews/sublayers of the navigation bar.
I've improved code from Achieving bright, vivid colors for an iOS 7 translucent UINavigationBar in my fork: https://github.com/allenhsu/CRNavigationController
With my modification, the result color on screen (picked on white background) will be exactly the same value passed into setBarTintColor. I think it's an amazing solution.
I know this answer is a little late, but if you're using Interface Builder, you might be getting the wrong color when using a hex value because Interface Builder is set to use the wrong color space. In Xcode 6.4, you can press the little gear in the top right of the color picker dialog to pick which color space you're using:
Mine was set to sRGB IEC6196-2.1, when I actually should have been using Generic RGB.
If your color isn't exceptionally vivid, you can calculate the equivalent color w/ alpha. This works well in iOS 7.0.3+; prior to 7.0.3 it automatically applied a 0.5 alpha.
This code assumes that your input color is RGB and is opaque, and that your background color is white:
- (UIColor *) colorByInterpolatingForBarTintWithMinimumAlpha: (CGFloat) alpha
{
NSAssert(self.canProvideRGBComponents, #"Self must be a RGB color to use arithmatic operations");
NSAssert(self.alpha == 1, #"Self must be an opaque RGB color");
CGFloat r, g, b, a;
if (![self getRed:&r green:&g blue:&b alpha:&a]) return nil;
CGFloat r2,g2,b2,a2;
r2 = g2 = b2 = a2 = 1;
CGFloat red,green,blue;
alpha -= 0.01;
do {
alpha += 0.01;
red = (r - r2 + r2 * alpha) / alpha;
green = (g - g2 + g2 * alpha) / alpha;
blue = (b - b2 + b2 * alpha) / alpha;
} while (alpha < 1 && (red < 0 || green < 0 || blue < 0 || red > 1 || green > 1 || blue > 1));
UIColor *new = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return new;
}
If anyone has a more elegant way of calculating the alpha (I'm cringing at that do-while loop) I'd love to see it: https://gist.github.com/sgtsquiggs/7206385
Just use this simple barTintColor Calculator:
http://htmlpreview.github.io/?https://github.com/tparry/Miscellaneous/blob/master/UINavigationBar_UIColor_calculator.html
Here is another way to get right color of translucent navigation bar in iOS 7.x and later. For some colors it's possible to find the optimal bar tint color that makes translucent bar to appear with color that matches the desired one.
For example, for Facebook color that is rgb: 65,96,156 or #41609c the optimal color is #21458c. The following code set all navigation bars in the app to be of Facebook color with native cocoa-touch API only:
UIColor* barColor = [UIColor colorWithRed:0.129995 green:0.273324 blue:0.549711 alpha:1.0]; // #21458c to make bars actual color match the #41609c color.
[[UINavigationBar appearance] setBarTintColor:barColor];
The only limitation of the method is that optimized color can't be found for every possible color. Usually this is not possible for dark colors.
I made an BarTintColorOptimizer utility that should be run on the device to search for optimized bar color for any color you enter.
I suppose you have read all the comments above. If you want to get the custom background & translucency you should override the navigationbar class and implement your own layoutsubviews method. Simple add additional subview here. IMPORTANT: you should add it just above the background subview of the NavigationBar. It will hide your header or buttons if you just put it above the all subviews.
Also, check out this question
Simple/fast solution that worked for me.
Just set the bar tint in the storyboard rather than the background.
First select your Navigation Bar in your Navigation Controller
And then click the attributes inspector on the right and then set the Bar Tint
You can select a pre-defined color or click on the color to set it to something else.
To make tint color look darker you can change the backgroundColor of the navigation bar:
UIColor *color1 = [UIColor colorWithRed:55.0f/256.0f green:0.0f blue:1.0f alpha:1.0f];
[navBar setBarStyle:UIBarStyleBlackTranslucent];
[navBar setBarTintColor:color1];
UIColor *color2 = [UIColor colorWithWhite:0 alpha:0.3];
[navBar setBackgroundColor:color2];
Try playing around with color1 and color2 to achieve the result that fits to you. Anything else would be fighting the framework.
navBar.barTintColor = [UIColor orangeColor];
navBar.translucent = YES;
UIColor *backgroundLayerColor = [[UIColor redColor] colorWithAlphaComponent:0.7f];
static CGFloat kStatusBarHeight = 20;
CALayer *navBackgroundLayer = [CALayer layer];
navBackgroundLayer.backgroundColor = [backgroundLayerColor CGColor];
navBackgroundLayer.frame = CGRectMake(0, -kStatusBarHeight, navBar.frame.size.width,
kStatusBarHeight + navBar.frame.size.height);
[navBar.layer addSublayer:navBackgroundLayer];
// move the layer behind the navBar
navBackgroundLayer.zPosition = -1;
Note you'll still need to muck with the the barTintColor and the backgroundLayerColor to get the exact color you want. Also, of course the colors depends on your content view's background color (e.g. white).
I've extented UINavigationController,
on its viewDidLoad, i've added a background with the same color of the tint.
Also, i've set the background frame to cover the status bar area:
self.navigationBar.barTintColor = navBackgroundColor;
CGRect bgFrame = self.navigationBar.bounds;
bgFrame.origin.y -= 20.0;
bgFrame.size.height += 20.0;
UIView *backgroundView = [[UIView alloc] initWithFrame:bgFrame];
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
backgroundView.backgroundColor = navBackgroundColor;
backgroundView.alpha = 0.6;
[self.navigationBar addSubview:backgroundView];
[self.navigationBar sendSubviewToBack:backgroundView];
On my case alpha 0.6 got the job done, but you can play with it.
If you're using swift 2.0 you can use this, this will remove the blur and have the color show properly.
UINavigationBar.appearance().translucent = false
From comparing my before an after colors I found there was a drop of 21% in saturation while Hue and Brightness remained the same.
By adding the 21% back on I was able to improve the color matching significantly. Unfortunately our color had a saturation above 80 to start with so pushing it above 100% had diminishing returns and didn't match perfectly but it got much closer.
For colors with saturation below 80 it should do even better.
For info on how to adjust saturation of your color How can I modify a UIColor's hue, brightness and saturation?
You can run the application in the simulator and take the color of the navigation bar using the Eyedropper tool.
i've looked a lot around and none of these has actually worked for me, the solution is as the following:
1- Set your navigation barTintColor (background).
2- Run the simulator and open your app, in your mac open Digital Color Meter and select the drop down to "Display in Generic RGB".
3- Use this tool to pick the navigation color that you will set for your view.
4- Go to storyboard and then select background color and make sure its on RGB Sliders, and put the color in here, you will get the exact same color of the navigation bar.
Note: it doesn't matter if isTranslucent is on or off.
Hope this helps you out.
This should work:
UIColor *barColour = [UIColor colorWithRed:0.13f green:0.14f blue:0.15f alpha:1.00f];
UIView *colourView = [[UIView alloc] initWithFrame:CGRectMake(0.f, -20.f, 320.f, 64.f)];
colourView.opaque = NO;
colourView.alpha = .7f;
colourView.backgroundColor = barColour;
self.navigationBar.barTintColor = barColour;
[self.navigationBar.layer insertSublayer:colourView.layer atIndex:1];
Taken from here
This works for me:
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, -20,navigationBar.frame.size.width,navigationBar.frame.size.height + 20);
layer.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.75].CGColor;
layer.zPosition = -5;
[navigationBar.layer addSublayer:layer];
This happens because the navigationBar.translucent == YES. Set this property to NO and it will be the correct color. However, I have not found out how to apply this translucent setting to all navigation bars without calling it explicitly on each. The status bar also stays the same color as the navigation bar this way.
Try rendering corresponding background image for your navigation bar. That worked for my test application.
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor colorWithRed:55.0f/256.0f green:0.0f blue:1.0f alpha:0.5f] set];
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
UIImage *navBarBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsLandscapePhone];

Customize uiswitch image properly?

I have an UISwitch in my iOS 6 app, that's on and off image is customised.
self.testSwitch.onImage = [UIImage imageNamed:#"on"];
self.testSwitch.offImage = [UIImage imageNamed:#"off"];
I use 77 points wide and 22 points tall image for this purpose (154x44 in retina), as it is stated in the documentation. But my image does not fit to my uiswitch, it seems ugly, the default style hides my one, like on the attached image.
What should I set to make it work in a proper way?
Here is code from my book. This is not exactly what you want to do, but it shows the technique and will get you started! Notice that I'm using 79 by 27 (not sure where you got your numbers from):
UIGraphicsBeginImageContextWithOptions(CGSizeMake(79,27), NO, 0);
[[UIColor blackColor] setFill];
UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,79,27)];
[p fill];
NSMutableParagraphStyle* para = [NSMutableParagraphStyle new];
para.alignment = NSTextAlignmentCenter;
NSAttributedString* att =
[[NSAttributedString alloc] initWithString:#"YES" attributes:
#{
NSFontAttributeName:[UIFont fontWithName:#"GillSans-Bold" size:16],
NSForegroundColorAttributeName:[UIColor whiteColor],
NSParagraphStyleAttributeName:para
}];
[att drawInRect:CGRectMake(0,5,79,22)];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.sw2.onImage = im;
Looks like this:
Apple doesn't have an Appearance API for UISwitch. You can set a tint color property (onTintColor). But that's not what you want, I guess. The problem about customizing UISwitch is that there's the opportunity that Apple will reject your app.
But there are some APIs for a custom switch such as RCSwitch(https://github.com/robertchin/rcswitch) or TTSwitch. A good tutorial and example of how to use RCSwitch can be found here: http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch.

UIButton's setTitleShadowOffset is deprecated

I am using UIButton's appearance proxy to customize all UIButtons in my application. Everything is working very well - I can set custom image, text color and shadow color. Only one thing is bugging me. To set shadow offset, I've used this piece of code:
[[UIButton appearance] setTitleShadowOffset:CGSizeMake(1, 1)];
And it's working. But the documentation says that setTitleShadowOffset: is deprecated, and instead we should use the shadowOffset property of the titleLabel. I've tried it like this:
[[[UIButton appearance] titleLabel] setShadowOffset:CGSizeMake (1.0, 1.0)];
but it's not working. Can I set shadow offset without using a deprecated method?
Try:
[[UILabel appearanceWhenContainedIn:[UIButton class], nil]
setShadowOffset:CGSizeMake(1.0, 1.0)];
([[UILabel appearance]
setShadowOffset:CGSizeMake(1.0, 1.0)]; should also work but is probably overkill as it will affect all UILabels, not just the ones contained in UIButtons.)

UIView clearColor produce WhiteColor instead of Transparent in IOS5 Xcode4.2

I am facing a very strange issue ever since I updated the Xcode with IOS 5 ...
I can't able to set the background Color of UIView to Transparent ...
here is What I can do :!!
I can set the view to any color , green , blue , red and they are all shown correctly on UIVIEW
[self.view setBackgroundColor:[UIColor greenColor]]; // Working 100 %
I can set a background Image // it's Working too
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"abc2010.png"]]]; // Working 100 %
[self.view setBackgroundColor:[UIColor clearColor]]; // No Error but result is not what I need
instead of transparent Background , it is always WHITE ... !!
I'll appreciate the help in figuring out the problem!! :)
I've just had this same problem. Here's a couple things to do.
Firstly, make sure you set the view so it's not opaque:
[self.view setOpaque:NO];
Second, set the views layer so it's also clearColor:
[self.view.layer setBackgroundColor:[UIColor clearColor]];
See if that helps. It fixed my problem, and your's sounds just like mine.
Good luck.

Resources