Back button on custom navigation bar - iphone

I am trying to develop an app with a navigation bar of custom size. (100px) In the navigation bar, I also want to include a back button. I have added both of them using the child view, but the back button is not working:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"menueButton.png"];
UIImage *backBtnImagePressed = [UIImage imageNamed:#"menueButton.png"];
backBtn.exclusiveTouch = YES;
[backBtn addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn setBackgroundImage:backBtnImagePressed forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(0, -40, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
[backButtonView addSubview:backBtn];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButton;
My navigation bar size is 100 pixels tall, and when I use button position (0,0,35,32) it will work correctly. But in this case, the button is being displayed much lower than intended. What am I doing wrong?

self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:#"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
check this link also.
Update:for back button
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}

Try allocing backButtonView like this
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 , 32)];
Change : orgin of view to (0,0)
Edit
Try
backBtn.frame = CGRectMake(0, 0, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];

Related

adding subviews in UINavigationBar with buttons doesn't work

I have a UINavigationController subclass and in the viewDidLoad I had the following code:
self.containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, self.navigationController.navigationBar.frameHeight)];
[self.containerView_ setBackgroundColor:[UIColor yellowColor]];
[self.containerView_ setAutoresizingMask:UIViewAutoresizingNone];
self.profileButton_ = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self.profileButton_ addTarget:self action:#selector(showProfileView:) forControlEvents:UIControlEventTouchUpInside];
[self.profileButton_ setCenterY:self.navigationController.navigationBar.frameHeight/2];
[self.profileButton_ setImage:[UIImage imageNamed:#"profile_icon.png"] forState:UIControlStateNormal];
self.socialButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.profileButton_.frameRight + 10, 0, 30, 30)];
[self.socialButton_ addTarget:self action:#selector(showSocialView:) forControlEvents:UIControlEventTouchUpInside];
[self.socialButton_ setCenterY:self.profileButton_.centerY];
[self.socialButton_ setImage:[UIImage imageNamed:#"social_icon.png"] forState:UIControlStateNormal];
self.notesButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.socialButton_.frameRight + 10, 0, 30, 30)];
[self.notesButton_ addTarget:self action:#selector(showNotesView:) forControlEvents:UIControlEventTouchUpInside];
[self.notesButton_ setCenterY:self.profileButton_.centerY];
[self.notesButton_ setImage:[UIImage imageNamed:#"notes_icon.png"] forState:UIControlStateNormal];
[self.containerView_ addSubview:self.profileButton_];
[self.containerView_ addSubview:self.notesButton_];
[self.containerView_ addSubview:self.socialButton_];
[self.navigationBar addSubview:self.containerView_];
I can see the UIButton but the container background is not even yellow color. When I press the button, the action of the UIButton is not called. Any idea why this is?
You are subclassing UINavigationController?
To quote Apple: "This class is not intended for subclassing."
The usual way to tweak the UINavController is to access the navBar from the perspecitve of a contained UIViewController. You would put the target/action methods in the UIViewController.

Adding a Custom Button to a UINavigationItem LeftBarButtonItem

Could anyone tell me why this code does not working?
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.backButton setImage:[UIImage imageNamed:#"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:#selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
[navigationItem setLeftBarButtonItem:backButtonItem animated:NO];
navigationItem.hidesBackButton = YES;
Edit:
Nothing appears on the leftBarButtonItem. That's the problem.
From the docs:
"When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value."
So you should see something if you set the frame in line 2, eg:
self.backButton.frame = CGRectMake(0, 0, 40, 20);
This should work
CGRect rect = CGRectMake(10, 0, 30, 30);
self.backButton = [[UIButton alloc] initWithFrame:rect];
[self.backButton setImage:[UIImage imageNamed:#"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:#selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
self.navigationItem.hidesBackButton = YES;

How to make visible a leftBarButtonItem created programmatically and set backgroundimage

Below code is working fine in my application. But that leftbarbuttonitem is invisible. And I can't set background image to leftBarButtonItem. I did some thing wrong, but I don't know what is the mistake? Is it possible to make visible and set background image?
UIImageView *NavigationBarImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
NavigationBarImage.image=[UIImage imageNamed:#"mapbutton.png"];
[self.navigationController.navigationBar addSubview:NavigationBarImage];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
-(void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
If you want to add a backgroundImage to your navigation item you have to add a custom button, like bellow:
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
tempButton.frame = CGRectMake(0, 0, 50, 30);
tempButton.showsTouchWhenHighlighted = YES;
tempButton.backgroundColor = [UIColor clearColor];
[tempButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"Back" forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithCustomView:tempButton];
self.navigationItem.leftBarButtonItem = backBtn;
And to add a background Image to navigation bar you can use the following code
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:#"YOUR_IMAGE.png"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
[home addTarget:self action:#selector(your_Action)
forControlEvents:UIControlEventTouchUpInside];
home.frame = CGRectMake(0, 0, 69, 26);
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:home];
[[self navigationItem] setLeftBarButtonItem:button2];
[button2 release];
button2 = nil;

UIBarButtonItem with image and text

I have to create a UIBarButton on the having a globe and a text "Map" written on it.
What I have done:
I have added a UIButton with the image of the globe as backgroundImage.
Set the text on the button.
Then created a UIBarButton initWithCustomView from the above button.
Given it style as Bordered.
Result:
Now what i got is a plane button with the globe image stretched on that which is overlapping with the Text in it.
I have searched for this a lot but i only found links which show the image in background and the text above the button. Nothing like the image and text together.
Can anyone help me in creating it the way it is shown in the attachment.
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Title" forState:UIControlStateNormal];
UIImage *butImage_on = [[UIImage imageNamed:#"closebutton_on.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
UIImage *butImage_over = [[UIImage imageNamed:#"closebutton_over.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage_on forState:UIControlStateNormal];
[button setBackgroundImage:butImage_over forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"globe.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(closeProfilePage) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 61, 30);
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 2)];
[button setImageEdgeInsets:UIEdgeInsetsMake(30,
20,
0,
20)];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
Note: you need to manage insets according to your requirements.
Use image with #2x. if your graphices are high resolution. according to 640 X 960 px;:-
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 6,51, 44)];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 51, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"back#2x.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:btnBack];
[btnBack release];
UIBarButtonItem *homeBarButtomItem2 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
[self.navigationItem setLeftBarButtonItem:homeBarButtomItem2];
[homeBarButtomItem2 release];

changing UIBarButtonItem UIButton's image

I have a UIBarButtonItem with a custom view of a UIbutton with an image, I want to have a different UIImage for different orientation, so here's my code:
UIButton * backButton = (UIButton *) ((UIBarButtonItem *) self.navBar_.topItem.leftBarButtonItem).customView;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD) {
[backButton setBackgroundImage:[UIImage imageNamed:#"back-landscape.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 46, 35)];
} else {
[backButton setBackgroundImage:[UIImage imageNamed:#"back-reading.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 23, 19)];
}
However this doesn't change the UIButton image, why is this?
Try this:-
UIButton * backButton ;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD)
{
backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 66.0f, 36.0f)];
[backButton setImage:[UIImage imageNamed:#"back-landscape.png"] forState:UIControlStateNormal];
}
else
{
backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 23, 19)];
[backButton setImage:[UIImage imageNamed:#"back-reading.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 23, 19)];
}
[backButton addTarget:self action:#selector(goToHome) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *HomeButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[self.navigationItem setLeftBarButtonItem:HomeButton];
You should notify that you say it is a UIBarButtonItem , but you use UIButton in code.
From ios5.0, you can use UIAppearence Protocol to change UIBarButtonItem background image, like:
[[UIBarButtonItem appearance] setBackgroundImage: forState: barMetrics:]
You can also use elppa's method, but notice you can directly use UIImageView for the customView parameter.
You could use the new proxy class with +appereance method check the DOC, but is only for iOS5. For earlier support you can update the image while the view is rotating the view controller method -willRotateToInterfaceOrientation.
Ciao
Create a UIBarButton using a custom UIButton:
_optionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_optionsButton setImage:[UIImage imageNamed:#"menu2.png"] forState:UIControlStateNormal];
[_optionsButton addTarget:self action:#selector(optionsButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * optionsItem = [[UIBarButtonItem alloc] initWithCustomView:_optionsButton];
Then set a background image as you would normally for that UIButton (instead of trying to change the image for the UIBarButton).
[_optionsButton setImage:[UIImage imageNamed:#"keyboard.png"] forState:UIControlStateNormal];
This worked great for me
Note: When I was going through this I was changing the picture depending on what the current picture was. I found I had to use:
if ([[_optionsButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed: #"keyboard.png"]]) {
// Code for the certain button function
}
to get it to work