Add a topbar to iOS application visible to all vie

2020-05-04 05:35发布

问题:

What is the best way to add a topbar(View) in a tabbar iOS application that always remain on top of all the views, irrespective of which tab is selected like the image below?

回答1:

I would use UIViewControllerContainment. Take a look at

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

The way I'd set it up is have a UIViewController that has two ContainerViews. One ContainerView will have the UITabBarController in it and the other would have the UIViewController for the top bar.

Here is what I did in a UIStoryboard



回答2:

Subclass UIViewController and change the title and title view in the viewDidLoad: method with something like this:

- (void)viewDidLoad
{
  [super viewDidLoad];

  if (!self.title || [self.title isEqualToString:@""]) {
      self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"]];
  } else {
      self.navigationItem.title = self.title;
  }
}

Then use this subclass on all the view controllers that you will have in your tabs.