Simple Title not showing up in UINavigationController
UINavigationController automatically shows the title of the UIViewController subclass it is displaying. It does so by looking at the navigationItem.title property of that UIViewController or UIViewController subclass. Basically UINavigationController doesn't have a title.
Can't set title of UINavigationBar
Don't add your own nav bar. Just put the view controller as the root of a navigation controller.
And there's no need to override loadView
like you are. And don't setup your app's root view like you do.
Try the following:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[RootViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
_window.rootViewController = nc;
[_window makeKeyAndVisible];
}
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.title = @"Title1";
}
@end
NavigationBar title doesn't appear
Hope this will help you out.
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
This code is working for me.
UPDATE : Swift 4/Swift 5
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.white
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
Navigation Bar Title Not Showing
Try to set the view controller's title property to "Hello"
self.title = "Hello"
If it works, you can find an explanation in AWebster's answer here Swift - Title Not Appearing for Navigation View Controller
UINavigationBar - Set title programmatically?
I've set the title programmatically using code something like this:
navBar.topItem.title = @"title";
where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.
If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.
UINavigationBar not displaying title in iOS
The problem here is your use of the UINavigationBar
.
Read the documentation for UINavigationController
programming here.
You should not be creating your own UINavigationBar
and adding it as a subview to your view controller's view.
The correct thing to do is to create a UINavigationController
and add your view controller to its stack.
So something like this:
CustomViewController *myViewController = ...// whatever initialization you do
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myViewController];
Each view controller has its own UINavigationItem
that the navigation controller uses to set up the navigation bar's view when that view controller is at the top of the stack.
So in your CustomViewController.m
, possibly in the init
method or in viewDidLoad
,
you can set up the navigation item by doing something like this:
self.navigationItem.title = @"whatever";
self.navigationItem.rightBarButtonItem = ...// create a UIBarButtonItem and set it here if you want a button on the right side of the navigation bar
So once again, you do not touch the navigation bar directly or add it to your view hierarchy. The UINavigationController
handles all of that for you.
iOS 11 large-title navigation bar not collapsing
Good news! I've just figured out that if I set "Large Titles" to "Never" on the storyboard, and then set it via code, then it works:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;
}
Seems like Apple forgot to handle the case when the navigation item has its largeTitleDisplayMode
set via the Interface Builder.
So until they fix this issue, leave "Large Titles" as "Never" on storyboards, and set them via code in viewDidLoad
.
You just need to do that to the first view controller. Subsequent view controllers honor the value in storyboard.
Related Topics
Swift Updating Screen in Between Steps of While Loop
How to Get Video Duration Fro Avasset in Swift
Array Search Function Not Working in Swift
How to Use Dispatchgroup/Gcd to Execute Functions Sequentially in Swift
Swift "Retry" Logic on Request
Getting Data Out of Completionhandler in Swift in Nsurlconnection
Uitextview Linktextattributes Font Attribute Not Applied to Nsattributedstring
Value' Is Inaccessible Due to 'Internal' Protection Level
How to Comprehend the "First-Class Function" in Swift
Uibutton Causing Unrecognized Selector Sent to Instance
How to Sleep for Few Milliseconds in Swift 2.2
How to Change Uitextfield Keyboard Type to Email in Swift
How to Make Uislider Default Thumb to Be Smaller Like the Ones in the iOS Control Center
How to Execute Different Implementation of a Method of a Generic Struct Based on the Generic Type
How to Know When to Call a Closure with () or Without Parentheses in Handler:
How to Create an Operator to Implement Error Chaining