UIBarButtonItem greyed out - iphone

I am adding two UIBarButtonItems to a UINavigationBar. The first of these is a system item - the search button - and works great. The second is a custom image (a small white Glyphish icon), but when I add this the image becomes grey. The button is perfectly clickable, just grey. Does anyone have an idea why this might be happening?
I do believe the alpha levels are correct. If I put the same icon in a UIBarButtonItem on a UIToolbar (rather than a UINavigationBar) this is how it appears:

Then, try cleaning the target.
Shift + cmd + K

It looks like it is in a highlighted state? If you're creating this button programmatically you might want to check if its UIControlState == UIControlStateHighlighted

You are using a toolbar type icon image. That Glyphish icons on toolbars and TabBars only use alpha layers. But if you are building a button you have to use a standard image. Open the image in any image editor and use the colors you want for your button and the transparent values for alpha layer where you want. It is, in some way, the inverse to the images made of alpha layers in tabbars.

I had been using a grey icon earlier. When I realized it was the wrong color, I suffixed the file with "-grey" and brought in a white icon, which took over the name of the grey icon. But somehow Xcode was still retrieving the old icon, despite its name being changed in the project (and thus in the Finder). Cached maybe? It's a mystery to me!
Thanks for your input everyone.

Try this to force reload button.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([sender isKindOfClass:UIBarButtonItem.class]) {
UIBarButtonItem *button = sender;
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = button;
}
}

Related

Custom, Imageless UIButton title disappears

I have created in the XIB a custom UIButton with no image. When the button is clicked, the title disappears.
These answers didn't help me because, I do not have any image that can overlap the text
After push a button it's title disappears
iOS - Interface Builder: UIButton title disappears when setting image
UIButton with custom background loses title
This didn't help either, because I do not have any background color
UIButton title disappears
This one didn't help too
Button changes title every time pressed
UIButton and its subclasses have 4 states which can change by code or stage config in IB
normal
highlighted
selected
disabled
Check UIButton.h in UIKit framework and you'll see how to use them:
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
If you setTitle, image, or backGroundImage for normal state, the default one of other states are same as normal one.
Default state is Normal, other state can be set:
[button setHighlighted:YES];
[button setSelected:YES];
[button setEnable:NO];//disable state
Button change from Normal to Highlighted on click, so if you want to keep normal title, please check:
-Don't use setImage or config image for normal and hightlighted state(just use backgroundImage)
-Don't setTitle:#"" forState:UIControlStateHighlighted or config to
nothing in IB.
-Don't setTitleColor or choose titleColor in IB same as UIButton backgroundColor.
You can test with 4 different titles and backGroundImages for 1 button and know how can it display.
In an XIB, you can set the title for each of the different states by changing the State Config and then setting the title for the corresponding state. In the following example, I have set the Default state title to Title and the Highlighted state title to a single space, which makes it appear as blank in the app. Note however, that the preview only shows the Default settings, and does not update for the different configurations (see images below).
Default State Title
Highlighted State Title
I have had this happen as well. What I believe the issue is, is since the button is default a white rounded button with the default "highlight" state of a button, and inverts the text color as the button is pressed, it does the same with a custom button. Now, since the text is a blue, and the background is white for a rounded button, it is quite the same in a custom button. The custom button's text will be whatever you want(set) it to be. The background is usually a transparent color as well, and since when the custom button is highlighted, it causes the text color to be inverted, just as a regular button does. This causes the textcolor to become transparent, and the background of the custom button just doesn't change because there is nothing to change to, hence the fact that the button is custom, (meaning you must set the highlight states and other attributes of the button). I have had this happen before, and I never really realized this before, but I hope this helps you out!
// for setting text in normal state
[_myButton setTitle:#"myText" forState:UIControlStateNormal];
// show another text on touch
[_myButton setTitle:#"myText" forState:UIControlStateHighlighted];

UIButton with changing images when pressed - but unattractive effect when changing them

I have a button that changes its image to different images depending on what modus we are in.
The images are set with:[modusBtn setImage:cx forState:UIControlStateNormal];
The images have round corners and the button is of Type "Custom".
Now when the button is pressed one can see another image in the upper and lower edge background.
Really weird - the buttons ALL have round corners - but for example when the blue modus is on and shows the blue image and one presses the blue button now - one can see red little edges only on the left two corners.
I already played around with all the button attribute settings in IB, but no luck.
Did anyone have a similar experience when changing button images?
So far all my button images where 100% rectangular and did not have round corners - therefore I never experienced this before.
Many thanks!
Use this UIButton's propriety for your button.
avatarButton.clipsToBounds = YES;
Whenever you set an image for custom button make sure to set
yourButton.backgroundColor=[UIColor clearColor];
The effect was happening as your button was using default background transitions during changing the state.

UIBarButtonItem Taking Entire Width of UIToolbar

I have a UIToolbar with a UIBarButtonItem that I created in Interface Builder. The UIBarButtonItem is a bordered button that contains a custom image.
The button takes up the entire length of the toolbar. I have tried using different images, changing the width of the button... I don't know what else to do. It stretches across the entire toolbar.
If I use one the provided "Identifiers" (camera, trash, compose...) the button appears a normal size.
How it looks in Interface Builder
How it looks when I run it on the iPhone
SOLUTION
After some more tinkering, here is what I found: if I create a new UIToolbar and add custom image to the UIBarButtonItem, it works fine - but as soon as I connect the UIBarButtonItem to an IBOutlet, it goes large. I had to then specify the UIBarButtonItem's width in the code, via the IBOutlet.
Try adding a UIBarButtonSystemItemFlexibleSpace on the right of the camera bordered item.
I'm not an expert, but I think it might have to do with how the frame is defined in the "sizing inspector", could you maybe show a screenshot of the settings?

in iPhone SDK How to change the color of BarButton Item

How to change color of leftBarButtonItem of UIBarButtonItem in NavigationBar?
if I am trying to set image in bar button through interfaceBuilder it shows border on image in bar button so is there any specific size for image for bar button or how to set image with-out showing border in it ?
If I am adding BarButton dynamically it doesn"t appear while running App
and if I am adding leftbarButton in xib only and connecting outlets then the leftbar button become visible but I am not able to assign customview view to it my ultimate goal is to change the color of left bar buttton
Please Help and Suggest,
Thanks
Create the bar buttom item with a custom view using -[UIBarButtonItem initWithCustomView:]. That way, you have all the control you want over the appearance.
I think you can use:
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
You can set custom image for your bar button.
If it's just the colour you want to change, add the button to the bar and then set the tint colour of the bar (see docs on UINavigationBar or UIToolBar). This of course changes the bar colour as well as the button however, but it's good practice (UI consistency wise) to use the same colour for UIBarButtonItems and their bars especially if you're using out-of-the-box buttons.

Half of UIButton not responding to touch events

I have a UIView - which I am using as a makeshift toolbar. It contains several UIButtons. (Six, I think) in a horizontal row.
The fifth button over will only respond to [TouchUpInside] events when I click the left half of it - whereas all the other buttons work correctly.
I have gone crazy making sure there weren't any other views overlapping it, etc - and everything seems okay.
Any idea of how to investigate further? Is there any way to see an "event inspector" to see where the touch messages may be going?
Most likely, the other half is covered by a transparent view that obstructs it. See the frames of all sibling views of the button:
for (UIView *v in myButton.superview.subviews){
NSLog(NSStringFromCGRect(v.frame);
}
And see if any of the frames above the button (the array is ordered from bottom to top) overlap with it.
If they don't, see if the whole window has any views covering the button.
The problem was as follows:
The "sixth" button was a "Info Light" type button. For some odd reason - even though the bounds of this button were clearly outside of the bounds of the offending "fifth" button - the "info light" button seems to acquire touches a bit outside it's bounding box.
When I either:
Moved the "Info Light" button further away from the "fifth" button
-or-
Changed the "Info Light" button to a regular (Round Rect) button
...the problem went away!
See the two rightmost buttons in this image:
I guess your superview bounds might be a bit too small? Try to change your superview background color to red or some vivid color and check its bounds.
Well you could try to use this technique with NSLog() to log events and get a sense of what might be happening. I'm not sure if there's a better way to log events:
Observing pinch multi-touch gestures in a UITableView
Just ran into this strange problem as well and after struggling with understanding the nature of the problem, ran across this post. Thanks for the helpful tip! I know it's an old question but this is how I was able to work around it in two ways. The first was to initially create the system info light button and simply retrieve the button image and create a custom button with that image, ignoring the initial button. The advantage to this is that you always get the latest info light graphic in case it changes in the OS. The disadvantage being that a new version of the OS might decide at some point to not make the image available in this way.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImage *img = [button imageForState:UIControlStateNormal];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:img forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:#selector(infoSelected:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[barItems addObject:barButton]; // NSMutableArray of tab bar items
The second approach, which I decided to use, so that I knew it would be available, and also to match my splash screen art in case the OS graphic changed, was to follow the same technique and simply save the original PNG to disk and add it to the project as a custom image for the button.
// Run these four lines just once in the simulator to save the original info
// light button image to disk and then eliminate these four lines and create the
// custom button with the image added to the project. Note that it may be a 1x or
// 2x image depending on the simulator device. So run it on both to get two
// versions (changing the name of course).
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImage *img = [button imageForState:UIControlStateNormal];
NSData *imgData = UIImagePNGRepresentation(img);
BOOL saved = [imgData writeToFile:#"/Users/Shared/InfoLight.png" atomically:NO];
I noticed that upon selecting the infoLight type, the button could no longer be interacted with. By checking the checkbox "User Interaction Enabled" I solved the problem.