How do I remove UIBarButtonItem glow programmatically? - objective-c

Unfortunately there is no showsTouchWhenHighlighted for UIBarButtonItem and I can't edit my button from the toolbar...

The property responsible for this is accessible in the UIButton class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.
Answer from here
EDIT:
Try this then:
UIImage* buttonImage = [UIImage imageNamed: #"header.navigation.back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width/2, 32);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backToPriorView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];

Related

Event on an Image in iOS

I am pretty new to iOS development, so this may seem to be a stupid question.. In my xib file, i have taken a UIImageView that will show an image when the page is loaded. But when a user taps on an image, I want the next view to be loaded.. But I am not able to do that because we cannot have a delegate not a target-action mechanism for a UIImage view.. Many places, I found a workaround.. using a button and then placing an image on it, and then using it.. but I want the first way to work.. Please help..
use UITapGestureRecognizer ,and Don't forget to set imageView.userInteractionEnabled = YES; because by default image not having UserInteraction.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
}
(OR) If you want
take button with BackGround image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
Instead of Using UIImageView, you can use UIButton.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setBackgroundImage:[UIImage imageNamed:#"your_image_Name"] forState:UIControlStateNormal];
[myBtn setBackgroundImage:[UIImage #"your_image_Name"] forState:UIControlStateHighlighted];
[myBtn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
also write button action:
-(void)buttonAction:(id)sender {
// Do whatever you want
}
In Case of XIB, Just use UIButton instead of UIImageView and set Background Image for both state Normal & Highlighted as you want. Rest you can manage button's action from XIB.
You can also add a fully transparent UIButton on top of your UIImageView... It should be preferred as each element has a clean semantics.

Show UIBarButtonItem just like Apple's iPad Mail App

Apple's iPad Mail app has a few icons that appear embedded in the setRightBarButtonItems area of the navigationItem of the detail view of the split view controller:
How can I add icons to the navigationItem bar like this (I have the icons already).
My issue is that the UIBarButtonItem class reference doesn't seem to have an appropriate UIBarButtonItemStyle that allows for no border around the button. I've tried configuring the UIBarButtonItem via initWithCustomView yet tapping the button doesn't work.
Thanks in advance for any suggestions
Cheers
EDIT: I could use UIBarButtonItemStylePlain however it doesn't look 'embedded' like the apple buttons, which is the look I am after.
I've worked it out and can produce the following result:
Here is the code I use. It could be optimised however is nice and repetitive for demonstration purposes:
- (void)setupNavigationItemButtons {
float buttonWidth = 60;
float buttonHeight = 40;
UIImage *imageA = [UIImage imageNamed:#"212-action2.png"];
UIImage *imageB = [UIImage imageNamed:#"111-user.png"];
UIImage *imageC = [UIImage imageNamed:#"122-stats.png"];
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
UIButton *buttonB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
UIButton *buttonC = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
[buttonA addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
[buttonB addTarget:self action:#selector(doSomethingElse:) forControlEvents:UIControlEventTouchUpInside];
[buttonC addTarget:self action:#selector(doSomethingRandom:)forControlEvents:UIControlEventTouchUpInside];
[buttonA setImage:imageA forState:UIControlStateNormal];
[buttonB setImage:imageB forState:UIControlStateNormal];
[buttonC setImage:imageC forState:UIControlStateNormal];
UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonB];
UIBarButtonItem *buttonItemC = [[UIBarButtonItem alloc] initWithCustomView:buttonC];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:buttonItemA, buttonItemB, buttonItemC,nil]];
}

How to change background image for navigation item(button)?

I want to set background image for every navigation item(button) (universal).
How should these images look?
Are there fixed dimensions or it is possible to create repeating background image?
How to programmatically change then this background image?
Thank's for help
UPDATE:
I found this code for changing background of this button but it's no working.
UIImage *barButton = [UIImage imageNamed:#"button_background.png"];
[[UIBarButtonItem appearance] setBackgroundImage:barButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
And I don't know which dimensions should be.
P.S. I want to change background of button just like you can change background color (self.navigation.controller.nvigationBar.tintColor..)
The code below might solve your problem
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"button_background.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = cancelButton;
u need to create Custom Back Button i m afraid
check this code out
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"yourImage.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
Let me know if it worked
Cheers
what about categories?
For example, you can do so:
#interface UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title image:(UIImage*)img;//add target and action if need
#end
#implementation UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title image:(UIImage*)img
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
//add background image and text on button, target and action if need, also
self = [self initWithCustomView:btn];
return self;
}

Touchable image as annotation callout?

I tried to use BarButton but this is wrong:
CGSize thumbSize = CGSizeMake(30.0, 30.0);
UIImageView *annotationThumbnail = [[UIImageView alloc] initWithImage:[((Sight *)annotation).thumbnail imageToFitSize:thumbSize method:MGImageResizeScale]];
UIBarButtonItem *annotationButton = [[UIBarButtonItem alloc] initWithCustomView:annotationThumbnail];
annotationView.leftCalloutAccessoryView = annotationButton;
UIBarButtonItem is not a subclass of UIView, so using an instance of UIBarButtonItem in a place that expects a view isn't going to work. Instead, you could just use annotationThumbnail like this:
annotationView.leftCalloutAccessoryView = annotationThumbnail;
If you instead use a UIControl subclass, like UIButton, map's delegate will get a -mapView:annotationView:calloutAccessoryControlTapped: message if someone taps the callout accessory:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:someImage forState:UIControlStateNormal];
button.frame = someRect;
annotationView.leftCalloutAccessoryView = button;

How to associate a method with this button?

I have a navigation bar button that displays both image and text. This is the code:
UIImage *saveImage = [UIImage imageNamed:#"star.png"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
saveButton.frame = (CGRect) {
.size.width = 100,
.size.height = 30,
};
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItem:barButton animated:YES];
I tried this
[barButton setAction:#selector(saveArray)];
but it doesn't work.
Remember that you must specify target as well. You can set action/target to the UIButton object itself:
[saveButton addTarget:target action:#selector(saveArray) forControlEvents:UIControlEventTouchUpInside];
I've tried to set target/action to the UIBarButtonItem directly but it seems not to work in case of UIButton for custom view for some reason.
Did you remember to set the target? If the desired method belongs to the same class, you could do:
[barButton setTarget:self];
That said, action and target are both properties (new to Obj-C 2.0), consider using the dot notation:
barButton.action = #selector(saveArray:)
barButton.target = self;