2771

UINavigationBar appearance addSubview not working in iOS 8

Question:

I want to add an image to the navigationBar. Ive used

[[UINavigationBar appearance] addSubview:logoImage];

it seems to work with iOS 7 but does not work with iOS 8, can anybody please explain why this is happening, and what is the alternate way to add an imageView on navbar in iOS 8?

Answer1:

Calling [[UINavigationBar appearance] returns an appearance proxy for the receiver class. The addSubview: method is not tagged as <strong>UI_APPEARANCE_SELECTOR</strong>. One major downside to UIAppearance's proxy approach is that it's difficult to know which selectors are compatible.

This article explains it a bit better: <a href="http://nshipster.com/uiappearance/" rel="nofollow">http://nshipster.com/uiappearance/</a> And this link <a href="https://gist.github.com/mattt/5135521" rel="nofollow">https://gist.github.com/mattt/5135521</a> from the article, shows the methods that are tagged as <strong>UI_APPEARANCE_SELECTOR</strong> in iOS 7.

You should subclass UINavigationBar and call [[UINavigationController alloc] initWithNavigationBarClass:toolBarClass:]. Pass your new navigation bar subclass as the first parameter and nil as the second parameter to use the standard UIToolbar. In your subclass, add the image just like you are doing.

Another possibility that may work is getting the navigation bar of your navigation controller and adding the sub view there, like this: [self.navigationController.navigationBar addSubview:logoImage]. This may work, but creating your own subclass will definitely give you more flexibility.

Recommend

  • Receive a notification (via BroadcastReceiver) when GPS (or location services) is started/stopped
  • Wait for a SLOT to finish the execution with Qt
  • Drop packet with libpcap
  • file_get_Contents won't send query strings in url , thus receiving nothing with $_GET
  • Use Mockito-core for create mock of final class?
  • AnalyticsReceiver in Google Analytics Tracking
  • Whitelist a set of properties from a multidimensional json array and delete the rest
  • How do I check assembly output of Java code?
  • Does Angular assign itself to `window.angular` globally, when loaded as CommonJS module?
  • Custom partiotioning of JavaDStreamPairRDD
  • Display Flycheck buffer (with errors) when saving
  • PHP function error and success pattern
  • Getting wrong values for other columns when I select MAX(updated_date)
  • Thread Synchronization with IntentService
  • Gzip http compression problem on iis7
  • Simple Injector fails if using verify before setting dependency resolver for MVC 4
  • Deployments not visible in Kubernetes Dashboard
  • Want to save selected (i.e., more than 1) enums as string with NHibernate
  • Deleting a widget from QTableView
  • Send emails through VB6 if no email client
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • ZipList with Scalaz
  • How do I formally document a C# Attribute in UML?
  • hide missing dates from x-axis ggplot2
  • Get specific string
  • Spring security and special characters
  • Regex thinks I'm nesting, but I'm not
  • Fill an image in a square container while keeping aspect ratio
  • Trying to switch camera back to front but getting exception
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • unknown Exception android
  • Programmatically clearing map cache
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app