13
votes

I have a UINavigationController based app. I can programatically set the title of the UINavigationBar from within my view controller's viewDidLoad method:

self.navigationItem.title = m_name;

Can I make it so that the title in the navigation bar is editable? IE. I want the user to be able to tap the title in the navigation bar, and to be able to edit it.

Is this possible? And if it is possible, does is it likely to meet Apple's Human Interface Guidelines? (This post suggests it will, but does not tell me how to implement it - Make UINavigationBar title editable)

Many thanks

Nathan

7

7 Answers

20
votes

You are able to set a custom view for your title.

titleView

A custom view displayed in the center of the navigation bar when the receiver is the top item.

@property(nonatomic, retain) UIView *titleView

Discussion

If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when the receiver is the top item. If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil.

Custom views can contain buttons. Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar. Custom title views are centered on the navigation bar and may be resized to fit.

If you were to place a UITextField in your titleView you could give it a clearColor background and set it to have no border. Wala you have a UINavigationBar Title you can tap and edit.

12
votes

Ryan's solution is exactly right. In case anyone is having trouble implementing it, here is some sample code that should get you going. Just paste it into ViewDidLoad on any class with a navigationController.

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)];
textField.text = @"Insert Title Here";
textField.font = [UIFont boldSystemFontOfSize:19];
textField.textColor = [UIColor whiteColor];
textField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = textField;

And if your project doesn't use ARC, make sure to release the textField like so:

UITextField *textField = [[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)]autorelease];

or like so:

[textField release];

Hope that helps.

2
votes

Ryan's solution for swift

let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 22))
textField.text = "Title"
textField.font = UIFont.systemFontOfSize(19)
textField.textColor = UIColor.whiteColor()
textField.textAlignment = .Center
self.navigationItem.titleView = textField
1
votes

Here is Ciprian's code in Swift 4 and corrected for iOS 12:

let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 21))

textField.text          = "Your Title"
textField.font          = UIFont.systemFont(ofSize: 17, weight: .semibold)
textField.textAlignment = .center

self.navigationItem.titleView = textField
0
votes

You can put a text field right ON TOP the navigation bar, and set it hidden/not enabled., the use a gesture recognizer to detect a tap on the navigation bar and set the text field to enabled/ not hidden. Then in the textfield's did end editing method, set it to hidden again and set navigation bar's title to that of the textfield.

0
votes

Add a hidden textfield over navigation bar, so on tapping, it will popup a keyboard and user can enter text into that.

Get the return button event, over there you can change your navigation bar text with newly added text. And hide the textfield again.

Enjoy Coding :)

0
votes

Put a textField on the NavigationBar and set the textField background color with opacity to zero.