I know my case may be rare but how do you disable the UIButton tint colour in this case?
I have a customised UIButton which has attributedTitle to help display the button pattern in different colour and alpha.
In my customised button .m file I have set something like this
[self setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateSelected];
which will make the background colour to gray when the button is selected
However the real result looks like this:
Some how the text colour gets turned into white which I think is because of the tint effect on UIButtons.
Is that possible for me to have the background as grey while the text still remain the colour as set in the attributed title on selected state of the button?
Thanks
yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];
(or)
If u placed button in storyboard....Choose button type as custom instead of system.
You can override setImage:forState: in UIButton subclass and change rendering mode to .alwaysOriginal.
override func setImage(_ image: UIImage?, for state: UIControlState) {
let newImage = image?.withRenderingMode(.alwaysOriginal)
super.setImage(newImage, for: state)
}
Simple, just head to the image in your assets, and set its rendering mode to "Original Image", check image below:
Follow this steps to get result:
First set UITabBar background color to clear
Now put this line in viewDidLoad method of the first page:
[[UITabBar appearance] setTintColor:SETYOURCOLOR];
Hope it will help you.
Related
I want to change background color of UISegmentedControl when it highlighted in tvOS.
Normally Segment display like following.
When change focus for change selected segment at that time display like following.
How to change white background when UISegmentedControl focused?
I was try following things but not working.
1) create custom class of UISegmentedControl and do following code in awakeFromNib
[self setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:image forState:UIControlStateFocused barMetrics:UIBarMetricsDefault];
2) override setHighlighted method of UISegmentedControl
3) change background color in didUpdateFocusInContext method.
From my understanding of your question, you want to change the background color same as UISegmentControl tint color. What you need to do is simply use this:
label.backgroundColor = segmentedControl.tintColor;
Hope this will help you if not try this:
You just need to give its background color to clear from storyboard. This is happening due to default background selection and default selection is as per theme. Once you set the segmentControl background color to clear you will get the required result.
Or you can do:
Try to set your UISegmentcontrol background color to clear in viewDidLoad, you will get your result. I have tried this and it worked for me.
segment.backgroundColor = Color.clear;
its easy to clear the background color of segment control:
yourSegment.backgroundColor = .clear
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 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
I have a set of buttons on a view controller that are added to the .xib and their properties are adjusted programmatically.
The background color is set in code as follows and then when it is tapped I change the background color using a selector method.
in configureButtons method:
[btn addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
which calls the following:
-(void) changeButtonBackGroundColor:(id) sender {
[sender setBackgroundColor:[UIColor colorWithRed: 199/255. green: 95/255. blue: 45/255. alpha:1.0]];
}
Adjusting the background when touched programmatically works fine when the "Highlight Adjusts Image" property is set to YES but it shows the white glow when touched as well as changing the background color.
I don't want to see the white highlight when the button is touched so I turned off the property. This causes the background color change to stop working.
Is there a way to change the background color of a UIButton without also showing the white glowing highlight when touched?
You can try using background images with particular color. In that case you can easily make use of – setBackgroundImage:forState: function. I think this will work even when showsTouchWhenHighlighted is set to NO.
I've got a bit of a dilemma. It's not a dealbreaker, but I'm interested in a decent answer if there is one.
I've been using a UIButton with a custom subview inside of a UIBarButtonItem (as the bar button item's customView). My custom subview is not a UILabel nor is it a UIImage, which is why I'm doing what I'm doing. This UIBarButtonItem is an item on my navigation bar, and the navigation bar has a tintColor set. I want the UIButton to have that rounded-rect appearance of UIBarButtonItemStyleBordered, which means it should also take on the tintColor of its parent bar.
Current solutions out there have implemented extracting png files from UIKit for UINavigationBarDefaultButton.png and family. This would look great if I weren't setting a tintColor property; instead the button remains that "iOS navy blue", when I want, say, bright orange (not the color I'm using but you get my drift). Which brings me to my dilemma: what's the best way to make a UIButton look and act like that UIBarButtonItem style, including taking on the tintColor property? I can set that property myself on the button; it's no big deal.
Would I want to draw that UIButton's background in CoreGraphics? Is there a framework/library out there that implements this already? If I'm dreaming the impossible, just tell me. I'm not that awesome with doing CoreGraphics by hand yet, but it's definitely not outside the realm of possibility.
If this can't be done, I know I can always take the cool custom UIView I'm attempting to finagle in and just save it off as an image (and thus use UIImage inside of a UIBarButtonItem), but I'd like to see if this is possible.
This is what I'm dealing with (below). The back button looks awesome, but only because I'm not messing with it. I'd like that rightBarButtonItem to use my tintColor that I have set, but in order to give it that UIBarButtonItemStyleBordered appearance with a customView set, I'm forced to make the button use png files for its background. Below is what you see, even with tintColor set on the UIBarButtonItem (since it's using a png).
I did find this question/answer on Stack Overflow, that gets me close enough that I can wing the rest. It's a well-detailed post about how to tint a UIImage with a UIColor; I've pulled it out into a function and I've posted a gist for that right here.
Combined with that, I've defined a category method on UIButton that lets me create a button with that background image, tinted, and I plop that into an empty UIBarButtonItem.
+ (id)buttonWithBarStyleAndTintColor:(UIColor *)tintColor customView:(UIView *)customView {
UIImage *defaultNormal = [UIImage imageNamed:#"UINavigationBarDefaultButton.png"];
UIImage *defaultPressed = [UIImage imageNamed:#"UINavigationBarDefaultButtonPressed.png"];
UIImage *back = [TintImageWithTintColor(defaultNormal, tintColor)
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
UIImage *pressed = [TintImageWithTintColor(defaultPressed, tintColor)
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
UIButton *button = [self buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 34.0f, 30.0f);
[button addSubview:customView];
customView.userInteractionEnabled = NO;
customView.frame = CGRectCenterRectInRect(customView.frame, button.frame);
[button setBackgroundImage:back forState:UIControlStateNormal];
[button setBackgroundImage:pressed forState:UIControlStateSelected];
[button setBackgroundImage:pressed forState:UIControlStateHighlighted];
return button;
}
You can try and set the UIButton type to custom and make sure its color is clear, same for your custom view, that way the only visible view will be the UIBarButtonItem tint color.
Also if you are willing to invest, there is this tool called PaintCode that does the code for CoreGraphics for you.