I have a UIBarButtonItem in a Navigation Bar with the text title "Save". When I transition to a fullscreen UIPopoverController and then dismiss it, the text in my UIBarButtonItem gets truncated to "S..e". For all other segues and views I have no problem when returning.
I've tried manually changing the width and setting "possibleTitles" to include long words but I can't stop the truncation.
I'm using a custom font if that makes a difference.
Try to init your UIBarButtonItem with a custom view.
[[UIBarButtonItem alloc] initWithCustomView:yourView];
Just make sure your custom view has the right frame (e.g. for an UILabel ,wide enough to not truncate its content). Things should work fine.
Perhaps helpful, but a UIBarButtonItem with a custom view (ex: UILabel) that is inserted into a UIToolbar can take on the intrinsic size of its contents as long as translatesAutoresizingMaskIntoConstraints is set to false. I believe this may work for UINavigationBar too:
private let barButtonLabel: UIBarButtonItem = {
let label = UILabel(frame: .zero)
label.translatesAutoresizingMaskIntoConstraints = false
return UIBarButtonItem(customView: label)
}()
I am trying to add three Bar button items using Flexible Space bar but the changes are not getting reflected in simulator. . I have pinned the navigation bar to bottom, left and right
If you use size class and auto layout, add constraints between your toolbar and the View: leading space = 0, trailing space = 0, bottom space = 0, heigh fixed.
You can add as much toolbar items as you wish until you have no space.
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:undo, done, cancel, nil]];
or
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:undo, done, cancel, nil]];
In iOS 7+, how would one go about setting the navigation bar title view to be a stack of actions that trigger sorting a mutable array within the view controller?
For instance:
Yik Yak's Navigation Bar
http://i.stack.imgur.com/PcJNM.jpg
You can set the navigation bar title to be a UIView with self.navigationItem.titleView. For example:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"New", #"Hot"]];
[segmentedControl sizeToFit];
// Configure your segmentedControl to your liking...
self.navigationItem.titleView = segmentedControl;
Also, can take a look at this one: UINavigationBar with buttons as title. Basically, you can customize the titleView as much as you want.
I am setting the title field of a UIViewController via Interface Builder/Storyboard:
This view controller is nested in a UINavigationController which in turn is nested within a UITabBarController. When I run the app, I my navigation item has no title, neither does the tab bar item.
If I explicitly set the view controller's navigation item's title, and also it's tab bar item's title in interface builder, then it works just fine.
I am wondering:
a)If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behaviour?
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
Thanks!
You can set the title of the UINavigationBar in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar.
Setting the title in code offers some different possibilities.
self.title = #"Your title"; will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.
self.navigationItem.title = #"Your title"; will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.
self.navigationController.title = #"Your title"; will set the title of a UITabBarItem but NOT the UINavigationBar.
Step 1
If you're looking at a Xib in Xcode's Interface Builder, take a look in the "Document Outline" panel (second panel from the left). Expand the view controller you're working with until you find an icon labelled: Navigation Item.
Step 2
If you then highlight the Navigation Item and open up the Utilities panel (the farthest on the right), and click the Attributes Inspector, you'll see where you can set the title of the view controller. This is the way to do it in Interface Builder, rather than doing it through code.
I ran into this issue this morning. Here are the stabs I took and the final workaround.
This correctly logs the child view controller's title as set in the storyboard, but has no effect on what's being presented:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Title: %#", self.title);
}
This has no effect; the title still doesn't show (probably doing an "if (![_title isEqualToString:title]){}" user the hood:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = [self.title copy];
}
This causes the title to be set correctly:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *title = self.title;
self.title = nil;
self.title = title;
}
My guess is that the UINavigationController is pulling the title for the view being presented before it has been loaded from the storyboard, but then doesn't start listening for changes until after the property has been set. I don't use storyboards or nibs very often, however, so it's quite possible there's a magic checkbox for this hidden somewhere that I've missed.
In any case, it looks like you can either do the self.navigationItem.title = self.title dance, or the above, as a workaround and still maintain your titles in IB.
Apples docs for this are kinda clear:
The navigation controller updates the middle of the navigation bar as
follows:
If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view.
To specify a custom title view, set the titleView property of the view
controller’s navigation item.
If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this
label is usually obtained from the title property of the view
controller itself. If you want to display a different title than the
one associated with the view controller, set the title property of the
view controller’s navigation item instead.
Emphasis mine.
I just ran into the same problem. I don't understand why it's not working... It might be on purpose or just be a bug.
To change the title in interface builder, you can click on the navigation item directly and change the title there:
Everything else on this page failed. For now, this worked, in code, in viewDidLoad:
NSString* text = #"My page title";
UIFont* font = [UIFont systemFontOfSize:20.0];
const CGSize SIZE = [text sizeWithAttributes:#{NSFontAttributeName:font}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SIZE.width, SIZE.height)];
label.text = text;
label.textColor = UIColor.blackColor;
self.navigationItem.titleView = label;
If you have UINavigationItem present, then you must use the navigation item title in the storyboard. If you don't have a navigation item for a view controller, then the UINavigationController will use the view controller's title property.
Example :
In your storyboard, if you have a navigation item the navigation controller doesn't need to read the view controller's title. Even if the title field is empty for this navigation item.
Remove the navigation item (if you can, you won't be able to do it for the root view controller but you will for the others) and your title will be correctly loaded
I think it works as designed although we expect another behaviour. If you print the title property in - (void)viewDidLoad it will be the same value that you set in story board so I see no reason of this not working unless Apple's choice.
a) If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behavior?
I believe this is the intended behavior. I think that the purpose of the title attribute of a view controller is more of a property that can be used at the developer's discretion perhaps for distinguishing between controllers.
Another reason for this change I think is that your navigation item's title may need to be different than the tab bar title, since the tab bar title cannot be nearly as long as the navigation title.
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
I think I mentioned this in my first paragraph. I think that the title attribute of a controller is a property that the developer can use perhaps for distinguishing between controllers.
I tried all of the above methods, even tried manually adding a navigation bar but to no avail.
So this is what worked for me.
Remove any navigation bar item you manually added to the view controller and add this to your viewDidLoad method
self.navigationController.navigationBar.topItem.title = #"My Title";
or
self.navigationController.topViewController.title = #"My Title";
In my case I solve with this:
[self.tabBarController.navigationItem setTitle:#"My Title"];
I currently have a button image for the leftBarButton of the UINavigationBar. Is there a way to eliminate the space between this button and the left edge of the UINavigationBar? I thought about just making a custom image for the background of the UINavigationBar, but I want to be able to use the back buttons that are generated on other screens. Ideas?
The layout logic of the navigation bar itself is going to want to try to maintain that space on the left side.
You could try specifying a custom view when setting the navigation items (UIBarButtonItem initWithCustomView:), and pass in a view with a negative x origin -- but I assume the navigation bar would ignore any such origin. (You could also try to keep a reference to that view, and move it to the left after the navigation bar finishes its layout)
Another option would be to try to create your own custom navigation bar -- possibly by putting a custom view in the center of the navigation bar, and stretching it to cover the width & height of the bar.
Try this, it works for me:
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.x = -10;
self.navigationController.navigationBar.frame = frame;
Basically, set the navigation bar x coordinate to negative not the bar item.
in ios7 you can just add a dummy barbuttonitem
for fixing left space you should add dummy as first, for right as last
example for left, you should add this after setting your original items or in viewdidload if you are setting buttons using storyboard.
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *spacerItem = [[UIBarButtonItem alloc] init];
[buttons addObject:spacerItem];
for(UIBarButtonItem *item in self.leftBarButtonItems){
[buttons addObject:item];
}
[self setLeftBarButtonItems:[NSArray arrayWithArray:buttons] animated:NO];