0

The Health App on IOS has a "user account" button next to each tab.

enter image description here

I would like to replicate this behavior. However, my code pushes the button above the title.

enter image description here

import Foundation
import UIKit

class MyTabController : UITabBarController {

    override func viewDidLoad() {
        view.backgroundColor = .lightGray
        viewControllers = [createNavController(for: UIViewController(), title: NSLocalizedString("Summary", comment: ""), image: UIImage(systemName: "suit.heart.fill")!)]
    }

    internal func createNavController(for rootViewController: UIViewController, title: String, image: UIImage) -> UIViewController {
        let navController = UINavigationController(rootViewController: rootViewController)
        navController.tabBarItem.title = title
        navController.tabBarItem.image = image
        navController.navigationBar.prefersLargeTitles = true
        rootViewController.title = title
        let barButton = UIBarButtonItem()
        barButton.image = UIImage(systemName: "person.circle")
        rootViewController.navigationItem.rightBarButtonItem = barButton
        return navController
    }
}

I know I can manually add a button with a label and push it "down", but it seems overkill for something so simple. What is an efficient way to reproduce this behavior? ( without introducing any static constants )

angryip
  • 2,140
  • 5
  • 33
  • 67

1 Answers1

3

If you try to scroll this slowly, you will see following.

Health.app setup does not seem like a standard UINavigationBar / UIBarButtonItem setup. enter image description here

Contacts.app shows what it looks like with a standard UINavigationBar / UIBarButtonItem setup. enter image description here

Add your own UIView below navigation bar, keeping the navigation title / bar button items empty. Track the scroll state and update your own view and navigationBar's appearances as needed.

Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30