UIButton's setTitleShadowOffset is deprecated - ios

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.)

Related

UISearchBar set translucent to NO doesn't work

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

Set rounded corners globally on UIButtons in iOS application

Is there a way to set UIButtons with rounded corners globally like with color below?
[[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];
The list of properties that you can set using UIAppearance is found here:
What properties can I set via an UIAppearance proxy?
Unfortunately rounded corners are not something that is possible.
You could use something like beautify (https://github.com/beautify/beautify-ios) which enhances the UIKit controls to allow you to specify rounded buttons.
With beautify, the following would give you rounded buttons globally:
BYTheme *theme = [BYTheme new];
theme.buttonStyle.border = [[BYBorder alloc] initWithColor:[UIColor blackColor]
width:2.0f
radius:5.0f];
[[BYThemeManager instance] applyTheme:theme];
I've found this link. Please see if it could help.
Taming UIButton
It is using this
[[basicButton layer] setCornerRadius:18.0f];
As i mentioned in previous answer. You have to subclass UiButton for it.. :)
#import <QuartzCore/QuartzCore.h>
Add this in your header file.
then in implementation,
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
cornerRadius will do the trick for you.. Let me know if more info needed.. :)
Edit
This cannot be achieved globally. As you used appearence, here is the list to see what you can customize with UIAppearance. what you can do is you can create a subclass of your UIButton, & there you can write implementation of setCornerRadius in initWithCoder Method.
Migrating advice for a workable solution to Swift (it's also possible using an equivalent ObjC category on UIView):
1. Add this extension https://gist.github.com/d3ce2e216884541217d0
2. Code:
let a = UIButton.appearance()
a.layerCornerRadius = 20.0
a.layerBorderColor = UIColor.redColor().CGColor
a.layerBorderWidth = 2.0
This sort of hack works because of how properties are copied. All appearance changes to things like a.layer and a.titleLabel are not propagated, but extension properties are copied.

iOS 7 UIButtonBarItem image does not tint

On my nav bar, I have a couple of rightBarButtonItems that have custom icons (the icon images are white, which worked well with the basic color scheme of iOS 6).
Under iOS 7, loading the images using initWithTitle (see code snippet 1) replaces the "white" color in the icon with the proper global tint (a specific color of dark blue in this case)
Code Snippet 1:
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:(UIBarButtonItemStyle) UIBarButtonSystemItemCancel target:(self) action:#selector(refreshList)];
refreshButton.image = [UIImage imageNamed:#"RefreshIcon.png"];
However, I needed to use initWithCustomView to overcome a weird change in behavior that was causing the icons to move out of view. The basic idea was to specifically set the size of the icons. initWithCustomView solved the sizing problem, but does not display the button images with the global tint, they are displayed in the color of the image (white). Code Snippet 2 shows how I am creating the button with initWithCustomView.
Code Snippet 2:
CGRect frameCustomButton2 = CGRectMake(0.0, 0.0, 18.0, 18.0);
UIButton *customButton2 = [[UIButton alloc] initWithFrame:frameCustomButton2];
[customButton2 setBackgroundImage:iconRefreshButton forState:UIControlStateNormal];
UIBarButtonItem *barCustomButton2 =[[UIBarButtonItem alloc] initWithCustomView:customButton2 ];
barCustomButton2.image = iconRefreshButton;
[customButton2 addTarget:self action:#selector(refreshList) forControlEvents:UIControlEventTouchUpInside];
All of this code is of course in (void)viewDidLoad. I have tried things like:
barCustomButton2.tintColor = [UIColor blackColor]; //doesn't work
or
[barButtonAppearance setTintColor:[UIColor blackColor]]; // doesn't work
and they do not override the white color of the image. It is almost as if the creation of the custom view takes place after the view looks at the global tint color?
How can I ensure the button icon takes on the global tint?
Thanks!
Just wanted to get this into a root comment to give better context to the "answer" checkmark, and give better formatting.
I was able to figure this one out! You can tell the image to always render as template, which will force it to take on the global tint color.
UIImage *iconRefreshButton = [UIImage imageNamed:#"MyIconFilename.png"];
iconRefreshButton = [iconRefreshButton imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
The default, if you don't set it, is "UIImageRenderingModeAutomatic" which means it will render as a template or original image based on context.
You'll either have to work around the issue you were having with the first code snippet, or you'll have to create a UIButton subclass that uses its image as a mask to show the tint color in drawRect:.
I'd recommend the first approach.

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];

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