UIButton is partially red when enabling Show Blended Layers - ios

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

Related

Center custom TitleView containing an UIImage and UILabel

I checked all the other answers, but I cannot get it to work using what they say:
I have a UIView myTitle, with Frame (0,0,320,44) which contains an UIImage and a UILabel. The UIImage and the UILabel are fine and look fine, but I cannot seem to figure out how to center myTitle.
self.navigationItem.titleView = myTitle;
It shows up and looks ok, but much too far to the left. It seems to (logically) be related to the left bar button (the back button) which is auto generated by the framework when I push a controller. That barbutton has a varying width based on that and it changes the x of myTitle view based on that.
So, where can I get the width of the left (and right) barbuttons so I can figure out how to put myTitle that in the center?
I notice when I change the width of myTitle it (logically) will center (because the UIImage and UILabel are centered within myTitle).
To have more control, instead of having the left bar button auto -generated, set it using code as below.
UIButton *leftButton =[UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setFrame:CGRectMake(0, 0, 100, 44)];
[leftButton setTitle:#"back" forState:UIControlStateNormal];
[leftButton setBackgroundColor:[UIColor greenColor]];
[leftButton addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftButton];
Now you can perfectly adjust the frame of titleView with out much problem.
I found two ways in which it works;
Like Cy-4AH suggested, using autolayout; her/his answer would be the answer
Not using autolayout it was much simpler than I thought; if i make the titleView Width to fit it's content exactly, it automatically works; is that done with auto layout internally?
Hope this helps someone.

UIButton colour changing

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

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.

Why can't I set backgroundImage of UIButton after removing all subviews

Just having some hard time with UIButton. I remove all subviews using
for (UIView *v in button.subviews) {
[v removeFromSuperview];
}
But I want to set the background image afterwards using
[button setBackgroundImage:[UIImage imageNamed:#"backgroundImage.png"] forState:UIControlStateNormal];
and nothing happens. If I do not remove all subviews than the previous code works.
So is it possivle that removing all subviews actually removes the backgroundImage as well, in which case, is it possible to put the backgroundImage back in?
Yes, according to what you experienced, the background image seems to be shown using a separate subview. Short answer: instead of raping poor view hierarchy (which is private anyway), you should keep track of the subviews you added yourself and remove only those.

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