Swift Tab Bar and multiple views

Good day, tell me good people! In my project, there is one Tab Bar Controller and 5 buttons in the bottom panel, each of which should logically open a WebView and a specific URL. How to do it correctly (programmatically or using the interface) what would not produce ViewControllers ? That is, to avoid doing 5 views, each of which will have its own WebView. Is it possible to make sure that when one of the five keys is pressed, the same viewcontroller opens and one is used webview? I tried Googling - but I didn't find the right solution... enter a description of the image here

Author: Сергей Федотов, 2018-06-04

1 answers

Instead of using the UITabBarController, you can simply use the UITabBar separately, placing it in the ViewController at the bottom. Only you need to take into account the margins in the WebView, because they will not be set automatically. Also, do not forget about the iPhone X - if you do everything correctly, the UITabBar itself will add a background below itself, and the binding to the bottom just needs to be done via SafeArea.

If you still need several ViewControllers, but just one class, then it is better to create a UITabBarController manually at startup after creating all 5 screens, set them to the created UITabBarController.

Here is a sample code:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {



        let rootViewController = UITabBarController()
        let viewControllers = (0..<5).map { 
          return WebViewController()
        }
        rootViewController.viewControllers = viewControllers

        window = UIWindow(frame: UIScreen.main.bounds)        
        window?.rootViewController = rootViewController
        window?.makeKeyAndVisible()

        return true
    }
}
 1
Author: ViR, 2018-06-04 14:01:30