Controller 1 displays an array of UIImages, Controller 2 manages the paging of those images once selected, and Controller 3 just displays the images for the pages. When I tap on a cell and load a photo into the photoViewController, the paging of images works fine, but my custom navigation bar is overridden with the default bar and I am unable to add any custom buttons to it or change any of its properties in code. The only thing I found that I am able to do is hide it. If I try to change the title of the back button or add my own title to the navigation bar nothing changes. Please help with anything you can.
i think you should try like this
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
and function could be
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 30)]; [backButton setImage:[UIImage imageNamed:#"bar_btn_bg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
if you fall in between let me know
Related
I am trying to customize the Navigation Bar by adding a custom button to the rightBarButtonItem. Creating one is pretty straight forward.
UIImage *myImage = [UIImage imageNamed:#"menu-icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(0, 0, myImage.size.width, myImage.size.height)];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[navItem setLeftBarButtonItem:myButtonItem animated:YES];
Looks good, except that now I can't set the target or action properties. The following code does nothing:
myButtonItem.target = myTarget;
myButtonItem.action = #selector(myButtonAction);
I found that the reason it's not working is because I initialized the UIBarButtonItem with -initWithCustomView. Apples documentation says:
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
I'm a little confused by what exactly apple means by that. Am I supposed to create a custom control or something? I haven't really done that yet and I'm not sure where to start.
Does anyone have any suggestions on how to do this or an alternative way to set up a custom Navigation Bar Button?
ANSWERED----------------------------------------------------------
Just had to add this method to the UIButton:
[myButton addTarget:myTarget action:#selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
It's pretty straight forward, you need to add target for uibutton not for uibarbuttonitem.
To explain more what you want for uibarbuttonitem is respond touch up inside action method, as documentation says that you need to do that for uibutton as you are initializing your uibarbuttonitem with custom view in your case uibutton.
Add an action and target to a UIButton like this:
[myButton addTarget:self
action:#selector(handleMyButtonTap)
forControlEvents:UIControlEventTouchUpInside];
Add an action and target to a UIBarButtonItem like this:
[myBarButtonItem setAction:#selector(handleBarButtonTap)];
[myBarButtonItem setTarget:aTarget];
With the help of answers by hgwhittle and Mustafa I implemented and it works perfect
here is the complete snip may help someone
//Create ImageButton
UIImage *shareBtnIcon = [UIImage imageNamed:#"dummy_share_icon_22"];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0, 0, shareBtnIcon.size.width, shareBtnIcon.size.height)];
[shareButton setBackgroundImage:shareBtnIcon forState:UIControlStateNormal];
[shareButton addTarget:self action:#selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//Create BarButton using ImageButton
UIBarButtonItem *shareBarBtn = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
//Add BarButton to NavigationBar
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:shareBarBtn, nil];
//method for share butto click
-(void) shareButtonClicked:(UIButton*)sender
{
NSLog(#"you clicked on share button %ld");
}
I want to remove back button title from my navigation bar and put a custom back button to make it appear as in ios7 (just a left arrow). I could change change the text, but when i change the text to nil, the previous page title appears again. Also changing the background image is not working too, though i could change the image
try this code to hide back button:
self.navigationItem.hidesBackButton=YES;
self.navigationItem.leftBarButtonItem=nil;
create custom back button like this:
CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
UIButton *back = [[UIButton alloc] initWithFrame:frameimgback1];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(back)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:back];
self.navigationItem.leftBarButtonItem = btnL;
I'm working in Xcode 4.3.2 using storyboards. I've created a segue between two views by clicking on the button I want to create a segue and control+click+drag a connection to the next view controller. Of course this creates an automatic/default back button in the navigation bar in the next view controller. What I want to do is customize the look of this back button. SO, I looked around online and found this code:
self.navigationItem.hidesBackButton = YES;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 40, 40)];
[button setImage:[UIImage imageNamed:#"homeButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = back;
It works great to change the LOOK of the button, but the problem now is that when I click that button, Xcode throws an error: "unrecognized error sent to instance..."
Can anyone help me to figure out how to now add the proper functionality to my custom back button? Thanks.
Have you implemented the method - (void)backAction?
To make the back button behave as such with a navigation controller, A possible implementation could look like:
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}
This may need to be tweaked depending on your app setup and what you want it to do when the button is pressed but it should be a good starting point
I am very new to iOS App Development.
In my app I am trying to navigate from second view to first view using navigation bar back button, but while drag and drop the navigation bar in the view I am just able to get the bar with title.
I am really worried and I am not sure what I am doing wrong? I am using Xcode 4.3. and storyboard to design the view.
What i am getting now
Looking for
Thanks for your help guys
Xcode handles this for you.
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
Here is one way to do it using code.
if you want to get a plainButton in the navigation like the iphone`s backButton (only one view), you must add image, i think there is no other way.Of course , if you have 2 or more viewcontrollers , you will use navigationItem.backBarButtonItem without add image .But the first viewcontroller do not have the backButton.
the code like this:
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
I would like a more elegant solution to changing the back button for an Application's Navigation Bar. All the answers I've seen only talk about customizing a button and then adding at least one line of code to each View Controller for the new button to display.
I have a category where I modify the Navigation Bar's background and Title. It's a category of UINavigationBar. The category calls willMoveToWindow: to call methods each time a new view is pushed on the Navigation Controller.
So here's how the code to create a new button would look:
- (void)applyCustomButton{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"arrow-left"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
}
The problem here is that self.navigationItem won't work here. I don't want to bore you with all the things I tried categorizing, but that property only works on View Controllers (I think)?
You don't have to give me a solution for the code above, maybe you have a better way of customizing the Back Button on the Navigation Bar without having to write code on every View Controller Class I have. I just hate repetition because maintaining code like that sucks.
Thanks in advance.