UIButton colour changing - ios

I am making an 'info' button, using Xcode's built in button type, "UIButtonTypeInfoLight".
This is my code:
self.helpButton= [UIButton buttonWithType:UIButtonTypeInfoLight];
[self.helpButton addTarget:self
action:#selector(showHelp)
forControlEvents:UIControlEventTouchUpInside];
self.helpButton.frame = CGRectMake(280.0, 440.0, 20, 20);
[self.view addSubview:self.helpButton];
However, there is a problem. My app contains a scroll view with 3 different view controllers. One blue, one red, and one green.
The icon looks fine on the page that the app opens up with (blue):
However, when i swipe to the green or red pages, the button seems to stay blue, and not transparent like I want it to be:
How can I stop it from doing this? I just want the icon to be transparent?

The UIButtonTypeInfoLight uses the application's tint color, which default to the blue you see. What you could do is change the UIButton's tint color on the fly:
[infoButton setTintColor:[UIColor redColor]];

Related

TintColor Changing on Popover Push

I'm setting the tint color of a window to an arbitrary color, then trying to over-ride this on a per-button basis, but it appears that the buttons revert to the window tint color whenever there is a segue applied on them.
Setting tint color in didFinishLaunchingWithOptions:
self.window.tintColor = [UIColor redColor];
and then my two buttons in viewDidLoad:
[self.button1 setImage:[[UIImage imageNamed:#"711-trash"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
self.button1.tintColor = [UIColor purpleColor];
self.button2.tintColor = [UIColor blueColor];
where button1 is a custom type button and button2 is a system type button.
When the popover first presents, the two buttons are tinted purple and blue. But when the segue view controller is pushed, the popped, the two buttons switch to red. Is there any way to prevent this?
EDIT:
I've tried reproducing your code like this:
However everything worked as expected:
So I agree with #user3779315, possibly you are setting the buttons' tint color somewhere else. Btw, additional code of your project would help to clarify the issue :-)

UIButton tint color affected the background color of the button in selected state

I have a button with typeSystem that has a specific image on normal state and a different one for a selected state. Problem is, when I set my button to selected state, the image won't show and the button's background is colored with the tint color.
Is there any way to disable it from happening and showing the image I set for selected state?
Instead of using buttonWithType:UIButtonTypeSystem you should replace it with self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom]; this will forego the tinted selection option you are getting. Then you can alter it's states images as the following:
[self.yourButton setBackgroundImage:[UIImage imageNamed:#"unselectedImage.png"] forState:UIControlStateNormal];
[self.yourButton setBackgroundImage:[UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateSelected];
Now the tint won't display and your selected/unselected images will retain that image until you alter the state again

UIButton is partially red when enabling Show Blended Layers

I would like to improve performance by making my views opaque where appropriate. I have a UIButton that is showing red in the simulator - it's only red around the text of the button, not the entire frame. In the Storyboard, I've enabled Opaque and changed the background color from clear to white, yet it still shows red in the simulator.
How do I change that to green so that it's fully opaque and not trying to work with transparency?
Note that UILabels are fully green when you change its background and opaque to YES.
I am use following code in your case:
[button.titleLabel setOpaque:YES];
[button.titleLabel setBackgroundColor:[UIColor whiteColor]];
// or which-you-want-color
Obviously, you should keep weak reference to your button.
Pretty works. Button size smaller than the screenshot size.
I believe the UIButton is made of a couple of views such as titleLabel. It may be possible to enumerate the UIButtons' subview and set them each to opaque.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
for(UIView *subview in [button subviews]){
subview.opaque = YES;
}

UIButton adjustsImageWhenHighlighted Property With Background Color

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.

Drawing a UIButton/UIBarButtonItem to use parent bar's tintColor?

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.

Resources