Here is the code I'm using:
-(void)viewDidLoad
{
NSString *checkerPath = [[NSBundle mainBundle] pathForResource:#"black-yellow-checker" ofType:#"png"];
UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
[checkerView setImage:checkerImage];
UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"yellowButton" ofType:#"png"]] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Marker Felt" size:30];
backButton.titleLabel.textColor = [UIColor blackColor];
[checkerView addSubview:backButton];
}
- (void)goBackHome:(id)sender
{
NSLog(#"go back pressed");
}
Not sure why this doesn't work. The button shows up, but when I press it nothing happens. It doesn't even change to the "indented" image when I touch it. Nothing. Can't use IB, has to be programmatically. Any thoughts?
UIImageView has userInteractionEnabled set to NO by default. This prevents any subviews from getting touches.
You could either enable user interaction of the image view by
checkerView.userInteractionEnabled = YES;
or create a new UIView to include both checkerView and the backButton.
UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
I believe that the forControlEvents: has to be UIControlEventTouchUpInside.
Related
I have an customised UINavigationcontroller created by me programatically.
The Problem is with the back button which comes as a default in UINavigationBar is not seen in IOS6 but when i press it the action can be done.
NOTE: The back button is seen in IOS5.
Here is my code that i had used
- (void)customizeNavigationController:(UINavigationController *)navController
{
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:keyNavBarTintColor];
UIImageView *myImageView = (UIImageView *)[navBar viewWithTag:keyNavBarBackgroundImageTag];
if (myImageView == nil)
{
UIImage *img = [UIImage imageNamed:#"image.png"];
CGRect rect = CGRectMake(0, 0, navBar.frame.size.width, navBar.frame.size.height);
myImageView = [[UIImageView alloc] initWithFrame:rect];
[myImageView setContentMode:UIViewContentModeScaleAspectFill];
[myImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
myImageView.image = img;
[myImageView setTag:keyNavBarBackgroundImageTag];
[navBar addSubview:myImageView];
[myImageView release];
}
self.navImageView = myImageView;
}
Try this:
UIImage *image = [UIImage imageNamed:kButtonBackInActive];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 39.0f, 44.0f)];
button.contentEdgeInsets = (UIEdgeInsets){.left=-8};
button.showsTouchWhenHighlighted = NO;
UIBarButtonItem * backbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[self.navigationItem setLeftBarButtonItem:backbutton];
When One of my app navigate from one controller to another, the 'go back' UIBarButtonItem disappear, so I wrote codes:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(barButtonItemPressed:)];
self.navigationItem.leftBarButtonItem = barButton1;
[barButton1 setImage:[UIImage imageNamed:#"fdj.png"]];
[barButton1 release];
It works, but it display as
but I prefer the style (like standard go back barbuttonitem
Is it possible?
Welcome any comment
You should edit the navigationItem of the parent view controller:
You can place this in the viewDidLoad of the previous view controller:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:#"fdj.png"];
here is the code how you can do this
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 55, 36);
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateNormal];
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = item4;
just make image and set it in uibutton, it's simple way to do this.
you can set any image you want.
hope this may help you what you want.
In the parent view controller's init method, set
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIimage imageNamed: #"fdj.png"]
style: UIBarButtonItemStyleBordered
target: nil
action: nil] autorelease];
This is what I'm using in an older project of mine (and yes, I know that stretchableImageWithLeftCapWidth is deprecated).
#implementation UIViewController (UIView_ExtenderClass) // this is a category, obviously
...
-(void)setLeftButton:(id)theTarget navItem:(UINavigationItem *)myNavItem action:(SEL)myAction title:(NSString *)myTitle;
{
CGSize titleWidth = [myTitle sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleWidth.width + 20, 32)];
[button setTitle: [NSString stringWithFormat:#" %#", myTitle] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
[button addTarget:theTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
UIImage *imgBack = [UIImage imageNamed:#"32_left_nav_bar.png"];
UIImage *imgBackStretched = [imgBack stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *imgBackSelected = [UIImage imageNamed:#"32_left_nav_bar_selected.png"];
UIImage *imgBackSelectedStretched = [imgBackSelected stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button setBackgroundImage:imgBackStretched forState:UIControlStateNormal];
[button setBackgroundImage:imgBackSelectedStretched forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttonItem setTarget:theTarget];
myNavItem.leftBarButtonItem = nil;
myNavItem.leftBarButtonItem = buttonItem;
[imgBack release];
[imgBackSelected release];
[button release];
[buttonItem release];
}
Call it in the viewDidLoad of the second controller, like this:
[self setLeftButton:self navItem:self.navigationItem action:#selector(yourActionHere) title:#"Back"]
The image I'm using is here: http://i49.tinypic.com/6qfzfs.png
When you tap on a UIBarButtonItem in a UIToolbar, there is a white glow effect.
Is there a possibility to fire an event to show this effect?
I don't want to press the button. Only the effect should be displayed. I want to visualize to the user, that there is new content behind this button.
Thanks for your help!
Heres the "highlight.png", I'm not kidding! (Though you may not seeing it on a white background.)
The following method assumes you are using the toolbarItems property of a navigation controller and that the button you want to highlight is the last item in that array. You can apply it to any toolbar, of course, and pick a different array index if necessary.
Right now this is not exactly perfect, because the animation moves the new button from the lower left of the screen. However, I think that can be turned off with a little effort.
Note that I used PeakJi's image.
I hope this works for you.
Enjoy,
Damien
- (void)highlightButton {
NSArray *originalFooterItems = self.toolbarItems;
NSMutableArray *footerItems = [originalFooterItems mutableCopy];
[footerItems removeLastObject];
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:#"white_glow" ofType:#"png"];
UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile];
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
imageView.frame = CGRectMake(0, 0, 40, 40);
UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[footerItems addObject:flashImage];
[UIView animateWithDuration:0.3
delay: 0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
self.toolbarItems = footerItems; }
completion:^(BOOL finished){
[UIView animateWithDuration:.3
delay: 0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.toolbarItems = originalFooterItems;
}
completion:nil];
}];
}
For Bar items
[(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:#"highlight.png"] forState:UIControlStateNormal];
In General - Assuming you have a button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(someFunction:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Click here" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
[self.view addSubview:button];
you can at any given point, programmatically call this function:
[button setTitle:#"Look Here" forState:UIControlStateNormal];
or if you like to have an highlight image
btnImage = [UIImage imageNamed:#"highlight.png"];
[button setImage:btnImage forState:UIControlStateNormal];
A very simple alternative:
That said , you can also set the button like this:
- (void)highlightButton:(UIButton *)button {
[button setHighlighted:YES];
}
You can try to recreate a new bar button item with different style, and then reassign it back. My experiment was:
In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Custom"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightBtn;
[rightBtn release];
[self performSelector:#selector(highlight)
withObject:nil
afterDelay:2.0];
}
And in method highlight:
- (void) highlight{
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Custom"
style:UIBarButtonItemStyleDone
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightBtn;
[rightBtn release];
}
Your implementation might be different than mine, but as long as you are fine with reassigning the bar button, I think it'll work as expected. Good luck :)
If you have a category:
#define HIGHLIGHT_TIME 0.5f
#interface UIButton (Highlight)
- (void)highlight;
#end
#implementation UIButton (Highlight)
- (void)highlight {
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
overlayButton.frame = self.frame;
overlayButton.showsTouchWhenHighlighted = YES;
[self.superview addSubview:overlayButton];
overlayButton.highlighted = YES;
[self performSelector:#selector(hideOverlayButton:) withObject:overlayButton afterDelay:HIGHLIGHT_TIME];
}
- (void)hideOverlayButton:(UIButton *)overlayButton {
overlayButton.highlighted = NO;
[overlayButton removeFromSuperview];
}
#end
Then you need code like this…
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
[self.navigationController.navigationBar.subviews.lastObject performSelector:#selector(highlight) withObject:nil afterDelay:2];
…to highlight the button in the navigation bar after 2 seconds.
A simple ay is to init your UIBarButtonItem with a CustomView (UIBUtton)
UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
[favButton setFrame:CGRectMake(0.0, 100.0, 50.0, 30.0)];
[favButton setBackgroundImage:[UIImage imageNamed:#"bouton_black_normal.png"]
forState:UIControlStateNormal];
[favButton setBackgroundImage:[UIImage imageNamed:#"bouton_black_hightlight.png"]
forState:UIControlStateHighlighted];
[favButton setTitle:#"Add" forState:UIControlStateNormal];
[favButton setTitle:#"Add" forState:UIControlStateHighlighted];
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[[favButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]];
[favButton addTarget:self action:#selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:favButton];
I have a UIToolbar.
If I create a UIBarButton like this, it works:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:iOB style:UIBarButtonItemStylePlain target:self action:#selector(myStuff)];
[button setTitle:#"Hi"];
If I create the UIBarButton like this, cause I want it to be colored:
UIImageView *vUP = [[UIImageView alloc] initWithImage:iUP];
UIBarButtonItem *UPButton = [[UIBarButtonItem alloc] initWithCustomView:vUP];
[UPButton setAction:#selector(anotherStuff)];
[UPButton setTitle:#"Hi";
[UPButton setTarget:self];
[vUP release];
The button shows, WITHOUT title and will doesn't respond to any touch...
am I missing something?
UIImage* image3 = [UIImage imageNamed:#"plain_btn-75-30.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
[someButton setTitle:#"Back" forState:UIControlStateNormal];
someButton.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:13];
[someButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UPButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
If you want image and title both then image must be set as a background image with title.
Use a UIButton instead of the UIImageView as a UIImageView does not respond to touch events.
You will, under no circumstance get a title as you provided a custom view.
I am trying to add an image to the UIBarButtonItem which I have to use in a UIToolbar.
I have managed to read the image and even get it to display with a UIImageView, but when i add it to the UIBarButtonItem and then add that item to the UIToolbar, the toolbar just displaces a "Blank White" space the size and shape of the image that I am trying to load.
here is what I am trying.
UIImage *image = [UIImage imageNamed:#"6.png"];
//This is the UIImageView that I was using to display the image so that i know that it is being read from the path specified.
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 50, image.size.width, image.size.height);
[self.view addSubview:imageView];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image forState:UIControlStateNormal];
//This is the first way that I was trying to accomplish the task but i just get a blank white space
//This is the Second way but with the same blank white result.
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2];
NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];
//Adding array of buttons to toolbar
[toolBar setItems:items animated:NO];
//Adding the Toolbar to the view.
[self.view addSubview:toolBar];
Your help will be much appreciated.
Thank You!
Shumais Ul Haq
Other than you'd normally expect in UIKit stuff, you might need to set a frame for the button explicitly. Maybe that's your problem.
This is what I wrote for a custom styled back button, as a category to UIBarButtonItem (but you can just take the pieces you need from it).
Note that this was used for the navigation bar, not the toolbar, but I presume the mechanics are the same, since it is a UIBarButtonItem as well. For UIToolbar you can just use IB to get it right at compile time.
#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f
+(UIBarButtonItem*)arrowLeftWithText:(NSString*)txt target:(id)target action:(SEL)selector
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [[UIImage imageNamed:#"arrow_left.png"]
stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchDown];
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[btn setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
[btn.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:FONT_SIZE]];
[btn.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];
/**** this is the magic line ****/
btn.frame = CGRectMake(0.0f,0.0f,
[txt sizeWithFont:[btn.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN,
IMAGE_HEIGHT);
[btn styleBarButtonForState:UIControlStateNormal withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateDisabled withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateHighlighted withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateSelected withImage:img andText:txt];
return [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
}
usage:
[UIBarButtonItem arrowLeftWithText:#"Back" target:self action:#selector(dismiss)];