I can change tint colour on UIButton but not UIImage - ios

I am playing around with tint colours on a UIImage and a UIButton.
On the UIButton (when the button type is set to System), I am able to set the tint colour of the UIButton in Xcode by going to Attributes Inspector -> View -> Tint
On UIImageView, I have set the same image, but when I go to the Attributes Inspector -> View -> Tint, the colour of the image does not change.
Why is the behaviour like this ? and how do I fix it ?
I am using iOS 7 and Xcode 5.1.1

Your image's rendering mode needs to be set to template and not original. To do this, you can call the -[UIImage imageWithRenderingMode:] method on your image and pass in UIImageRenderingModeAlwaysTemplate, then set your image view's image to the resulting image:
yourImageView.image = [yourImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
Or, in Swift:
yourImageView.image = yourImage.imageWithRenderingMode(.AlwaysTemplate)

Related

How to make a UITabBarItem ignore the tintColor

I have a UITabBar with 5 tabs.
I set an "unselectedImage" to tabBarItem's image property and "selectedImage" to tabBarItem's selectedImage property.
The "selectedImage" is an image with a gradient, but it presents it in the color of the tabBar.tintColor.
So how can I cancel the tintColor and make the original image to be presented (without coloring it with the tintColor)?
Thanks!
When you set the image, define the rederingMode:
yourUIImage.renderingMode = alwaysOriginal

how to set alpha on Backgroung image using Swift

I have a button with background image and need to set the alpha to 0.5. How can I do this in swift?
This is my code that set the button background image
centerButton.setBackgroundImage(UIImage(named: "buttonImage"), forState: UIControlState.Normal)
Thanks
if you want to set alpha on only background image, then re-create UIImage with changing the alpha value.
if you want to set alpha to entire button, then simply set alpha value to the button.
centerButton.alpha = 0.5
I think you must create UIImage instance first and change alpha, then, setBackgroundImage to your button.
You can check: How to set the opacity/alpha of a UIImage?

Programmatically set UIBarButtonItem fill color?

I have a set of UIBarButtonItems from png files inside a UIToolbar.
When a user clicks on an icon, I want that icon to be filled with a color to indicate a state change.
Is it possible to do this with a single set of images (maybe by programmatically changing some attributes) or do I necessarily need two sets of images (on for each state) ?
make sure your UIImage's renderingMode is UIImageRenderingModeAlwaysTemplate first
if you initWithImage then change UIBarButtonItem's tintColor.
Although UIBarButtonItem is not a view, its tintColor property behaves
the same as that of UIView.
else if you initWithCustomView in which with a UIImageView then change the imageView's tintColor might work too
else you can setItems: animated:NO to UIToolBar every time after clicks which might not be a elegant one
It is very straight forward. You just need to do the following in code :
imgView.image = [imgView.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imgView.tintColor = [UIColor someColor];

Why does a UICollectionViewCell with a UIButton have a monochrome/tinted image?

I create a UICollectionView and add a single cell who's only subview is a UIButton. That button has its title and image set. I've verified that the image data is correct in the debugger.
When the button is drawn on screen I see the text and the image however the image looks as if it has been filled with the tint color, obscuring all of the image other than its shape.
What am I missing here to have this show up as a normal button should?
Update
It turns out this is not specific to UICollectionView but rather all UIButtons in iOS7.
iOS 7 makes all images in buttons behave as template images using the alpha channel of the image in concert with the tint color to produce the image (much like the images in a tab bar). There's a new renderingMode property on UIImage which is defaulted to "automatic" which lets the context decide (which is template style for buttons)
This can be circumvented using the new imageWithRenderingMode: method on UIImage:
UIImage* myImage = [UIImage imageNamed:#"Foo.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[button setImage:myImage forState:UIControlStateNormal];
The easiest way to avoid this is to use a different UIButtonType. It's UIButtonTypeSystem on IOS 7 that has this behaviour, so you could use a custom button instead:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:myImage forState:UIControlStateNormal];
When the UIButton's background color lightText in this way, it will not close the button image.
UIButton.backgroundColor = UIColor.lightText

Custom IOS UITabbarController with hidden top gray line

I'd like to make a view with a unique color with uitabbar i.e I don't want to separate the view into the UITabbar and the rest, so I've created a custom UITabbar programmatically with custom color. The UITabbar and the "rest of the view" have the same color but there is a gray line on top of the UITabbar that separates the to parts. How can I hide that?
this is an example image, I want to delete that dark line:
https://picasaweb.google.com/felixdl/20Giugno2012#5756005463317234882
SOLUTION
Thank you! this works perfectly! the line I've added is:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"myImage.jpg"]];
I've never used the "appearance" tag before
If you're building for iOS 5, you can set the background as an image which would eliminate the grey line you're talking about.
[uiTabBarController setBackgroundImage:[UIImage imageNames:#"my_background.png"]];
This does require you to have an image which matches your programitically created color though.
In iOS4, you can override the drawRect function (which is significantly more complicated, but I'd be happy to answer if you're making a pre iOS 5 app)
Try this,
** Objective-C **
//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];
// or
// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];
** Swift **
//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil
// or
// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()
Here is apple document for shadowImage.
#available(iOS 6.0, *)
open var shadowImage: UIImage?
Default is nil. When non-nil, a custom shadow image to show instead of
the default shadow image. For a custom shadow to be shown, a custom
background image must also be set with -setBackgroundImage: (if the
default background image is used, the default shadow image will be
used).

Resources