0

Context: I have a custom tab bar controller, with a center tab bar item for the main action of the app. Tapping this item presents a new view controller modally as a page sheet.

Issue: When tapping this item AND only on first tap (immediately following app launch), there's a slight delay presenting the view controller. After the first tap, and after waiting some time the lag disappears. However since it's the main CTA of the app, it's not a good user experience to feel this first delay.

Testing: I've created a new app to single out this functionality only, and it's still happening. I've also add a few prints statements that all print on time, it's really just the delay in the presentation.

Thanks!!

Below the code:

class PopUpViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
  }
}


class CustomTabViewController: UITabBarController, UITabBarControllerDelegate {

  var viewController: PopUpViewController?

  override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
  }

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    addCenterUITabBarItem()
  }

  func addCenterUITabBarItem(){
    viewController = PopUpViewController()
    var sfConfig = UIImage.SymbolConfiguration(paletteColors: [.white])
    sfConfig = sfConfig.applying(UIImage.SymbolConfiguration(font: .systemFont(ofSize: 34.0)))
    let iconImage = UIImage(systemName: "plus.circle.fill", withConfiguration: sfConfig)
    
    let newTabBarItem = UITabBarItem(title: nil, image: iconImage, tag: 3)
    viewController?.tabBarItem = newTabBarItem
    newTabBarItem.imageInsets = UIEdgeInsets(top: -10, left: 0, bottom: 10, right: 0)
    viewControllers?.insert(viewController!, at: 1)
  }


  func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController.isKind(of: PopUpViewController.self) {
        return false
    }
    return true
  }

  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("prints on time")
    let indexOfTab = tabBar.items?.firstIndex(of: item)
    if indexOfTab != 1 {
        selectedIndex = indexOfTab! // todo
    } else {
        print("prints on time")
        
        if let promptVC = storyboard?.instantiateViewController(withIdentifier: "custom-vc") as? PopUpViewController {
            let nav = UINavigationController(rootViewController: promptVC)
            nav.modalPresentationStyle = .pageSheet
            
            if let sheet = nav.sheetPresentationController {
                sheet.detents = [.medium(), .large()]
            }
            print("prints on time")
            self.present(nav, animated: true, completion: nil)
        }
    }
}

}

robinyapockets
  • 363
  • 5
  • 21

0 Answers0