iOS 8 Text Field Tint Color - ios

In iOS 7, changing the 'tint color' attribute of a uitextfield would change the cursor color of that text field. In iOS 8, even when I change the global storyboard tint color, this does not occur (objective-c, still works in iOS 7). How do I fix this?

I just tried to replicate your problem but both on iOS7.1 and iOS8 the tintColor attribute of a textfield works perfectly.
This line of code change the cursor color of the textField. Try this instead of changing the tint color in Storyboard
textField.tintColor = [UIColor colorWithRed:98.0/255.0f green:98.0/255.0f blue:98.0/255.0f alpha:1.0];
Hope it helps!

try the following:
[[self.textField setTintColor:[UIColor blueColor]];
[self.textField setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];

Looking to actually put a tinted color filter on the whole view, not just change cursor color?
Look no further. .tint is a lame name because it in no way implies that it adjusts the cursor color. Naturally, people googling about the .tint property are likely trying to find a way to apply a color filter across the whole frame/region of their UIView, UITextView, whatever.
Here is my solution for you:
I made macros for this purpose:
#define removeTint(view) \
if ([((NSNumber *)[view.layer valueForKey:#"__hasTint"]) boolValue]) {\
for (CALayer *layer in [view.layer sublayers]) {\
if ([((NSNumber *)[layer valueForKey:#"__isTintLayer"]) boolValue]) {\
[layer removeFromSuperlayer];\
break;\
}\
}\
}
#define setTint(view, tintColor) \
{\
if ([((NSNumber *)[view.layer valueForKey:#"__hasTint"]) boolValue]) {\
removeTint(view);\
}\
[view.layer setValue:#(YES) forKey:#"__hasTint"];\
CALayer *tintLayer = [CALayer new];\
tintLayer.frame = view.bounds;\
tintLayer.backgroundColor = [tintColor CGColor];\
[tintLayer setValue:#(YES) forKey:#"__isTintLayer"];\
[view.layer addSublayer:tintLayer];\
}
To use, simply just call:
setTint(yourView, yourUIColor);
//Note: include opacity of tint in your UIColor using the alpha channel (RGBA), e.g. [UIColor colorWithRed:0.5f green:0.0 blue:0.0 alpha:0.25f];
When removing the tint simply call:
removeTint(yourView);

Related

How to give #3f51b5 color code programmatically for shadow color in ios

I want to make shadow color with #3f51b5.
How can I do that. hope your help. this type cannot use to do that.
cell.layer.shadowColor = [UIColor purpleColor].CGColor;
you need to set shadow first
cell.layer.shadowRadius=8;
cell.layer.shadowOffset=CGSizeMake(-1, -1);// this make top left shadow
cell.layer.shadowOpacity=1;
cell.layer.shadowColor = [UIColor colorWithRed:0.2471 green:0.3176 blue:0.7098 alpha:1.0].CGColor;
after setting the color give shadow radius,offset and opacity, convert the color to rgba and use this
cell.layer.shadowColor = [UIColor colorWithRed:0.2471 green:0.3176 blue:0.7098 alpha:1.0].CGColor;
cell.layer.shadowRadius=10;
cell.layer.shadowOffset=CGSizeMake(1, 1);
cell.layer.shadowOpacity=1;
if you want to use hex color only use this
How can I create a UIColor from a hex string?

UIWebView's content background color not transparent

I'm using an UIWebView to load some HTML text from a plist.
I managed to make its background transparent using
webView.opaque = NO;
webview.backgroundColor = [UIColor clearColor];
but the content, which is the text loaded from the plist, is shown with a disturbing white background.
I'm loading it using
NSString *descr = [NSString stringWithFormat:#"<html><head><style> body{font-size:16; font-family: Helvetica; color: black;} strong{font-family: Arial; font-weight: bold; font-size:16} .bg{background:transparent;}</style></head><body><div class='bg'>%#</div></body></html>",[item valueForKey:#"description"]];
[webView loadHTMLString:descr baseURL:nil];
I've tried setting the .bg class background color to rgba(255,0,0,0.0), but nothing worked.
Also tried setting webView.scrollView.backgroundColor : [UIColor clearColor];
Any advice on how to get the UIWebView's content background transparent?
try this !
[webView setOpaque:NO];
webView.bakgroundColor = [UIColor clearColor];
In XCode 6.x uncheck Opaque and change Background's opacity to 0%. I think other XCode versions will also work.
Your HTML body will still have a background color.
You need to add 'background-color:transparent;' to the body part of your CSS
See here: https://stackoverflow.com/a/3935033/78496

How can I get the iOS 7 default blue color programmatically?

I'm creating custom elements in my app and want to match the look and feel of the new iOS. iOS 7 introduced to us a very common lighter blue color, the default color or tint for several elements, including the system button, segmented control, etc. They've made it easy to select the color using IB, as seen here:
However, I haven't found how to easily access the color programmatically. I checked out the UIColor documentation, and there doesn't seem to be any accessor for the blue system color in the class itself.
Here's my question: does a simple accessor exist for this color? [UIColor ?] or something like it? If not, does someone know the exact RGB values for that color?
Use self.view.tintColor from a view controller, or self.tintColor from a UIView subclass.
It appears to be [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0].
iOS 7 default blue color is R:0.0 G:122.0 B:255.0
UIColor *ios7BlueColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
According to the documentation for UIButton:
In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information.
Assuming you don't change the tintColor before grabbing the default value, you can use:
self.view.tintColor
Here is a simple method to get the default system tint color:
+ (UIColor*)defaultSystemTintColor
{
static UIColor* systemTintColor = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIView* view = [[UIView alloc] init];
systemTintColor = view.tintColor;
});
return systemTintColor;
}
Hex Color code
#007AFF
and you need this libary
https://github.com/thii/SwiftHEXColors
ps. iOS, Swift
swift 4 way:
extension UIColor {
static let system = UIView().tintColor!
}
Native extension with predefined system colors gives what you're looking for:
// System colors
extension UIColor {
/* Some colors that are used by system elements and applications.
* These return named colors whose values may vary between different contexts and releases.
* Do not make assumptions about the color spaces or actual colors used.
*/
...
#available(iOS 7.0, *)
open class var systemBlue: UIColor { get }
...
}
You can use it directly:
myView.tintColor = .systemBlue
Get the color automatically by using this code:
static let DefaultButtonColor = UIButton(type: UIButtonType.System).titleColorForState(.Normal)!
The UIWindow.tintColor method wasn't working for me in iOS8 (it was still black), so I had to do this:
let b = UIButton.buttonWithType(UIButtonType.System) as UIButton
var color = b.titleColorForState(.Normal)
This gave the proper blue tint seen in a UIBarButtonItem
From iOS 7 there is an API and you can get (and set) the tint color with:
self.view.tintColor
Or if you need the CGColor:
self.view.tintColor.CGColor
In many cases what you need is just
[self tintColor]
// or if in a ViewController
[self.view tintColor]
or for swift
self.tintColor
// or if in a ViewController
self.view.tintColor
Please don't mess with view.tintColor or extensions, but simply use this:
UIColor.systemBlue
while setting the color you can set color like this
[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0]
Adding a category to UIColor the following way will make it available to you anytime you need it or even change its definition accross your code:
#interface UIColor (iOS7Colors)
+ (instancetype)iOS7blueColor;
#end
#implementation UIColor (SpecialColors)
+ (instancetype)iOS7blueColor;
{
return [UIColor colorWithRed:0.0f green:0.22f blue:122.0/255.0 alpha:1.0f];
}
Once you import the Category in your code you can call the color by using:
UIColor *myBlueColor = [UIColor iOSblueColor];

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];

UIBarButtonItem: how can I change the text shadow offset?

I'm trying to change the offset of the shadow behind the text in a UIBarButtonItem.
This is my code:
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithWhite:0.30 alpha:1.0] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Changing the text color works. Changing the shadow color works. Changing the shadow offset doesn't seem to do anything.
Is something wrong with the way I'm doing this? I've also tried setting it directly, without the appearance proxy, but that didn't work either.
I believe your code is correct though perhaps you expect something different from what it does. If I paste it into a test app and change the offset values to 10.0, 10.0 the shadow offset is visible for me. (iOS 5.0)
Your shadow UIOffset values are 0.0,0.0 which effectively causes no shadow. You will need to offset the shadow by at least 1 pixel in any direction. eg. this will give you a shadow to the bottom left side.
[NSValue valueWithUIOffset:UIOffsetMake(-1.0, 1.0)]

Resources