<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tewha.net</title>
    <link>https://tewha.net/tags/uinavigationbar/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 17 Oct 2013 14:00:00 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/uinavigationbar/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Change back button title</title>
      <link>https://tewha.net/2013/10/change-back-button-title/</link>
      <pubDate>Thu, 17 Oct 2013 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/10/change-back-button-title/</guid>
      <description>&lt;p&gt;When you’re using a navigation controller, the title of the back button on a particular view controller is pulled from the view it leads to.&lt;/p&gt;
&lt;p&gt;Although this can be initially confusing, this actually makes a lot of sense. If two different view controllers (say, Circles and Squares) might push the same view controller (Details), shouldn’t the text in the top left of that view controller depend on which controller pushed it (Circles or Squares)?&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When you’re using a navigation controller, the title of the back button on a particular view controller is pulled from the view it leads to.</p>
<p>Although this can be initially confusing, this actually makes a lot of sense. If two different view controllers (say, Circles and Squares) might push the same view controller (Details), shouldn’t the text in the top left of that view controller depend on which controller pushed it (Circles or Squares)?</p>
<p>Think of it this way:</p>
<pre><code>thisViewController.visibleBackButtonTitle =
    previousViewController.navigationItem.backBarButtonItem.title
    ?: previousViewController.navigationItem.title
    ?: previousViewController.title;
</code></pre>
<p>This won’t compile; <code>visibleBackButtonTitle</code> doesn’t actually exist.</p>
<p>There are a few reasons to show custom text on the back button.</p>
<h2 id="more_generic">More generic</h2>
<p>The most obvious reason is that your title relies on user input that may be too long, and you want something simpler and more generic.</p>
<p>This might be the case if the user taps an event. The title in the navigation controller should be the event’s title, but you might want to use something shorter like Event on the back button.</p>
<p>This case is simple: select the parent view controller and give it a custom <strong>Back Button</strong> value.</p>
<h2 id="more_specific">More specific</h2>
<p>You might also have the opposite problem, where your context is not visible in the navigation title because it’s obvious from something else in the view controller. But you still want to leave that more specific title as a breadcrumb for the user.</p>
<p>An example of this might be an address without a label. The first line of the address doesn’t make sense as a navigation title, as it duplicates the view. But if you tap deeper than that, you might want to include <em>something</em> about the address, such as the house number of the position in the list.</p>
<p>This is a little more complicated. You’ll need to do this at runtime: Set the <code>title</code> of the <code>backBarButtonItem</code> for the navigation item of the view controller the button leads to. That will prevent the navigation item’s <code>title</code> from being used.</p>
<p><strong>But there’s a wrinkle.</strong> Let’s assume you want to set the title dynamically. You might have something like this in your parent view controller, which is called by your <code>viewWillAppear</code> and whatever triggers updates of the view controller:</p>
<pre><code>- (void)loadData {
    // lots of other stuff
    self.navigationItem.backBarButtonItem.title = @&quot;dynamic title&quot;;
}
</code></pre>
<p>This probably isn’t going to work for you as is.</p>
<p>You’ll find lots of crazy solutions to this; manipulating the <code>backBarButtonItem</code> by hand and restoring it after for instance. (There are much, much crazier solutions.)</p>
<p>The key is understanding why it’s not working: <code>backBarButtonItem</code> is <code>nil</code>.</p>
<p>To make it not <code>nil</code>, you have two options:</p>
<ol>
<li>Select the navigation item in the storyboard editor and give <strong>Back Button</strong> a value. This value will cause the <code>backBarButtonItem</code> to be automatically created with the view controller is instantiated.</li>
<li>Create the <code>backBarButtonItem</code> at runtime in your <code>viewDidLoad</code> or <code>awakeFromNib</code> (<a href="https://twitter.com/calebd/status/391068542076207105">suggested by @calebd</a>). I don’t recommend doing this in code; you’re already using a storyboard or nib, so it’s much simpler to just put a <strong>Back Button</strong> value into your view controller.</li>
</ol>
<p>Once it’s created — whether in code or by storyboard — its title can be changed. You can change <code>backBarButtonItem</code>’s title at any time when the parent is displayed, as often as you like: it’ll only be shown when you push something else on top of it.</p>
<h2 id="summary">Summary</h2>
<p>If the text is always the same:</p>
<ol>
<li>Select the parent view controller’s <strong>Navigation Item</strong> in the editor.</li>
<li>Put the text into the <strong>Back Button</strong> value.</li>
</ol>
<p>If the text is dynamic:</p>
<ol>
<li>Select the parent view controller’s <strong>Navigation Item</strong> in the editor.</li>
<li>Put <em>some</em> text into the <strong>Back Button</strong> value.</li>
<li>Set the title in the <em>parent</em> view controller, when its contents change: <code>self.navigationItem.backBarButtonItem.title = dynamicText;</code></li>
</ol>]]></content:encoded>
    </item>
    <item>
      <title>Changing UINavigationBar’s title text color</title>
      <link>https://tewha.net/2010/10/changing-uinavigationbars-title-text-color/</link>
      <pubDate>Fri, 01 Oct 2010 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2010/10/changing-uinavigationbars-title-text-color/</guid>
      <description>&lt;p&gt;How do you change UINavigationBar’s title color? You can’t, directly, but you can substitute your own view.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>How do you change UINavigationBar’s title color? You can’t, directly, but you can substitute your own view.</p>
<p>For example, start with <a href="http://developer.apple.com/library/ios/#samplecode/NavBar/Introduction/Intro.html">Apple’s NavBar sample</a>. Drop this code into <code>initWithNibName:bundle:</code> in <code>PageThreeViewController.m</code>:</p>
<pre><code>- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        CGRect frame = CGRectMake(0, 0, 400, 44);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor yellowColor];
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@&quot;PageThreeTitle&quot;, @&quot;&quot;);
        [label release];
    }

    return self;
}
</code></pre>]]></content:encoded>
    </item>
  </channel>
</rss>
