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?
Related
I tried to set with empty image, but it not working
navigationController?.navigationBar.shadowImage = UIImage()
setting background image & shadow images working for me. Thanks #xingou
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
this is what the API document said :
The default value is nil, which corresponds to the default shadow image. When non-nil, this property represents a custom shadow image to show instead of the default. To show a custom shadow image, you must also set a custom background image with the setBackgroundImage(_:for:) method. If the default background image is used, then the default shadow image is used regardless of the value of this property.
so you need set the backgroundimage first.
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
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];
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)
I have a button that has contentMode set to UIViewContentModeScaleAspectFill. I set it like this:
self.itemImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
I also set it to Mode - Aspect Fill in IB for the button, just in case. And when I set the image, I DON'T set is as a background image:
[self.itemImage setImage:image forState:UIControlStateNormal];
However, occasionally my image does not fill the button (which I want it to do), like in this case:
See grey spaces on each side. Occurs on both iOS 6 and 7.
Any ideas?
At long last, I managed to find the solution that works in Alfie's answer to this SO question.
In short, if your UIButton (or its hidden imageView) does not respond to contentMode, use this code:
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;