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
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.
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.
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
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
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.
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 :)
Put a textField on the NavigationBar and set the textField background color with opacity to zero.
Related
I am building iOS8 app. On my tableview controller, I am using self.navigationController.hidesBarsOnSwipe = YES, to hide the navigationBar on swipe up gesture. It is working nicely, but my statusBar becomes transparent and shows the table content underneath.
On storyboard, Status Bar are Top Bar are set to "Inferred"
I want to:
1. Keep my status bar opaque
2. Maintain the same color as the navigationBar
3. Table content scrolls underneath the statusBar
Thank you.
Here is a Swift solution:
First, change UITableViewController to UIViewController and add a tableView field.
Then, implement your viewDidLoad method as follows:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.frame
view.addSubview(tableView)
let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
topBar.backgroundColor = myDesiredColor
view.addSubview(topBar)
}
You can add a constraint to the top layout, by this scrolling content will not appear below the status bar.
Make a custom View.
UIView * statusBarView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:statusBarView];
I am trying to create a UIButton programmatically instead of using the interface builder. I initialize and set the button frame but the button doesn't seem to appear on the view. My code is as follows:
UIButton *showInfoButton = [[UIButton alloc] init];
CGRect buttonFrame = CGRectMake(0,0, 150, 150);
showInfoButton.frame = buttonFrame;
What am I missing? Please bear with me as I am new to iOS.
You forgot to add your UIButton as a subview to the UIView you want it to display on. Since you are building UI programmatically, you need to manually add the button as a subview to the container view.
You can do so by using this line of code:
[self.view addSubview:showInfoButton];
The button might not still appear as you are making a custom button which has a transparent background colour. In order to see the button, you can set the UIButton's backgroundColor property as follows:
[showInfoButton setBackgroundColor:[UIColor blackColor]];
I want to change the titleView of navigationBar with a regular text field. Then I want to set the textField size to fill the old "normal" titleView.
I can't seem to be able to do this in storyBoard. Dragging a text Field to the place where the navigation title View is doesn't work.
So I added stuff at
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PO(self.navigationItem.titleView);
CGRect theFrame= self.navigationItem.titleView.frame;
self.navigationItem.titleView=self.searchBar;
//self.searchBar.frame = theFrame;
while (false);
...
It's working with one cached. That PO is a macro that print the content of the object. Turns out at viewDidAppear, self.navigationItem.titleView is null.
So while I can display the searchBar, I cannot make the searchBar "fill" it's space because I do not know the space is.
I prefer not to hard code it because you know, things may change in the future.
So what should I do?
I once saw codes where rather than setting the self.navigationItem.titleView, you would simply add subview to it. The problem with this approach even on viewDidAppear, self.navigationItem.titleView is 0.
I added these codes:
CGRect theFrame= self.navigationItem.titleView.frame;
CGRect theFrame2 = self.searchBar.frame;
CGRect theFrame3 = self.navigationController.navigationItem.titleView.frame;
And, I do not know how to nslog structure value, however, theFrame and theFrame3 are all 0
You can try this inside viewWillAppear:
UIView *customTitleView = [[UIView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor clearColor];
//create your UITextField or UILabel or other view and add as subview of customTitleView
self.navigationItem.titleView = customTitleView;
I have changed my navigation bar color via the following code
navconFvc.navigationBar.tintColor = [UIColor colorWithHexString:#"faf6f5"];
The code worked but the text color also needs to be changed (see screenshot below). Also the refresh button logo on the right is affected as well
The same issue occurs if I navigate to another page in the stack
Question: How can I change the color of the
title text
Back button text and
right bar button icon color?
After I changed the background color of the navbar?
In iOS 7, just use:
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
Change [UIColor whiteColor] with whatever text color you want
For the title here's the way:
iPhone Navigation Bar Title text color
And for the custom buttons here's the way:
adding buttons to ui navigation controller bottom bar
To change text color:
_navController.navigationBar.titleTextAttributes
= #{UITextAttributeTextColor : [UIColor blackColor]};
Adding refresh button and color it:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(reload)];
[button setTintColor:[UIColor blackColor]];
self.navigationItem.rightBarButtonItem = button;
Variables that effect navigation bar background:
_navController.navigationBar.backgroundColor = [UIColor whiteColor];
_navController.navigationBar.tintColor = [UIColor whiteColor];
_navController.navigationBar.translucent = NO;
I just put together a simple UIViewController subclass that adds a customizable back button that allows you to change text colors. It basically adds some willAppear/willDisappear logic to animate the back button the way the UINavigationController does while using the leftBarButtonItem property. You might extend this to also do the rightBarButtomItem as well.
https://github.com/typeoneerror/BBCustomBackButtonViewController
i found a lot of topics around placing buttons on navigation bar but none showed me how to place a button at the center of the bar.
I found an example with a segmented control but i don't need that.
Just a button instead of the title. Is that possible? ;-)
Edit:
That will do it:
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
self.navigationItem.titleView = titleButton;
You can set any custom view (e.g. UIButton) as a titleView property to your current navigationItem - that should do the trick.