iOS 7.1 update hide navigation bar left button by default - ios

I have following code to create UINavigationBar and set the navigation item with a back button at the right side.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
navBar.delegate = self;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonTapped)];
UINavigationItem *backItem = [[UINavigationItem alloc] init];
[backItem setTitle:#"What's New"];
[backItem setLeftBarButtonItem:back];
backItem.leftBarButtonItem.enabled = YES;
[navBar pushNavigationItem:backItem animated:NO];
[self.view addSubview:navBar];
This worked perfectly until i update my xCode 5 to iOS 7.1 update recently.
But now when the UIView presented the navigation button is not visible. But when i touch the location of the button (where it used to be before the update), it shows me the button and click even is firing.
My question is how to set the button visible at the moment view is present to user ?
Thank you.

Try to initialize your UIBarButtonItem with initWithCustomView. Also, try to set buttonType of the UIButton to UIButtonTypeSystem.

Thanks to all,
finally what happen is as follows.
The above behaviour is not what i assume it is. When a new view is loaded the button is disappear by default and only if i move my finger here & there on the location button used to be it appears. Further when i keep the view still, after it loaded, about 15 seconds, button appears.
So i suspect this is with "viewDidLoad" method. (Code related to this UINavigationBar is in viewDidLoad method.
When I move above code to "viewDidAppear" method it start to work again.

Related

Navigation Item's click or touch event sometimes does not being fired

Most of the time navigation item in navigation bar works fine.
Navigation bar is hidden before user taps screen.
Navigation bar moves using [[self navigationContoller] setNavigationBarHidden:YES/NO animated:YES]
However, sometimes navigation item does not fire touch up inside event.
This is snippet of one of them.
[self.button addTarget:self action:#selector(someFunc) forControlEvents:UIControlEventTouchUpInside]
Button animates fade out and in just like being touched fine, but event does not being fired.
But when I make navigation bar to hide and emerge again by tapping screen, navigation items works fine again.
What's the problem? I have totally no idea.
Thanks in advance for your help.
I solved this problem by using UITapGestureRecognizer and UIImageView.
I added both default UIBarButtonItem and customized UIBarButtonItem using UIImageView and UITapGestureRecognizer to navigationBar for test. Then when I faced the problem again, the default UIBarButtonItem did not respond but customized UIBarButtonItem worked fine.
This is my sample code.
UIImage buttonImage = [UIImage imageNamed:#"buttonImage" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
UIImageView buttonImageView = [[UIImageView alloc] initWithImage:buttonImage];
UITapGestureRecognizer buttonTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(targetFunction)];
[buttonImageView addGestureRecognizer:buttonTapGesture];
UIBarButtonItem *bbiButton = [[UIBarButtonItem alloc] initWithCustomView:buttonImageView];

Resign First Responder UITextView. Temporary UINavigationBar?

I am writing an application where I let the user edit some content in a text view, and I want to add a dismiss button, to let the user save there edits, without hacking around to dismiss the keyboard.
I am familiar with dismissing the keyboard programmatically and everything. The only real problem I have now, is a design problem. I want to let the user click a button just like in the notes app, but I don't have a UINavigationBar.
Is it possible to create a temporary Navigation bar and assign it a "done" button?
I am going to implement this in a custom UIView, so it should be controller independent. What I mean is I don't know if there is a UInavigationBar present in the current controller. So that must be dealt with dealt with before adding the temporary one.
Any help is appreciated.
Something like this would work nicely for what you're describing:
In viewDidLoad:
UIToolbar *inputToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonTapped)];
[inputToolbar setItems:[NSArray arrayWithObject:doneButton]];
self.theTextView.inputAccessoryView = inputToolbar;
In doneButtonTapped
[self.view endEditing:YES];

How to create Navigation bar with back button on it?

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];

How do I specify an action for a custom Back button in a standalone UINavigationBar?

I have a custom UINavigationBar on a screen, i.e., no navigation controller, as defined below which contains a Back button w/ the title "Media" and the action "mediaViewComplete". However, the mediaViewComplete method is not being called when the button is tapped. How do you specify an action for a Back button?
self.navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
navigationBar.barStyle=UIBarStyleBlackTranslucent;
UINavigationItem *navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:#"Media" style:UIBarButtonItemStylePlain target:self action:#selector(mediaViewComplete)];
navigationItem.backBarButtonItem=backButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];
navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *editButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editCategorization)];
navigationItem.rightBarButtonItem=editButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[editButton release];
[backButton release];
[navigationItem release];
The problem is that you are using:
navigationItem.backBarButtonItem = backbutton;
The backBarButtonItem has it's own event that is not overridden by your action: option. In fact, Apple guidelines state that you should use "nil" as you action for backBarButtonItem. The backBarButtonItem is not owned by the current view controller.
If you are unconcerned about the arrow shape of the button, you should use leftBarButtonItem instead. If you want to create fully custom back buttons with the arrow shape, you'll have to do some custom magic. Luckily, most of the work has been done for you:
http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/
Download this project and look at the results. It even contains the images you need to retain an arrow shaped custom back button. It is more work, but it's much less hack.

UIToolbar in iPad

I wanted to have two buttons on the both end of Navigation Bar (in iPad's Detail View Controller).
So I created two UIToolbars and I set the them as Left&RightBarButtonItems.
But, There is some color variation in the NavigationBar.
Attached images for your understanding.
the code I used ,
UIToolbar *leftToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
NSMutableArray *lItems = [[NSMutableArray alloc] initWithArray:[leftToolbar items]];
UIBarButtonItem *lb1 =[[UIBarButtonItem alloc]initWithTitle:#"Home"style:UIBarButtonItemStyleBordered target:self action:#selector(home:) ];
UIBarButtonItem *lb2 =[[UIBarButtonItem alloc]initWithTitle:#"New Document"style:UIBarButtonItemStyleBordered target:self action:#selector(newDoc:) ];
[lItems insertObject:lb1 atIndex:0];
[lItems insertObject:lb2 atIndex:1];
[leftToolbar setItems:lItems animated:YES];
[lItems release];
leftToolbar.barStyle =UIBarStyleBlackTranslucent;
leftToolbar.tintColor=[UIColor clearColor];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftToolbar];
Can you help me to avoid this color variation?
Is there any other way to have buttons like this, without using UIToolbar?
Thanks ,
Gopi.
just remove navigation bar and add tool bar, why you adding toolbar to navigation bar?
To achieve the same, use segment controll, set it in Left or right barbutton's view, once you select a segmet, deselect it after few seconds, say 0.3secs, it looks good, no color vairations, it looks like a part of Navigation bar
Found the solution !
code is correct, but one small bug. Have to set the height to 44, not 45. I did this and it seems to fit over the existing NavigationBar.
UIToolbar *leftToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
Works for me . . Anyway I moved to the Single tool bar method.
Hope this helps some one.!!
Have a great day!!
Gopi.

Resources