Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar editable title?

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

like image 935
Nathan Russell Avatar asked May 20 '12 13:05

Nathan Russell


4 Answers

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.

like image 180
Scott Kohlert Avatar answered Nov 13 '22 10:11

Scott Kohlert


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
like image 20
meaning-matters Avatar answered Sep 19 '22 13:09

meaning-matters


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.

like image 20
Ryan Poolos Avatar answered Nov 13 '22 10:11

Ryan Poolos


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
like image 2
Ciprian Avatar answered Nov 13 '22 10:11

Ciprian