简体   繁体   中英

How to reuse same UIView in all viewcontrollers in iOS App

Hi I'm new to iOS Development, I'm developing an iOS Application in that I want to use same footer throughout the app. Now I'm creating different uiviews in every controller but this is not optimal way. How can I create single uiview and reuse it in app. I'm using storyboards.In my footer I have four buttons

Create your reusable view as a XIB and load it programatically where ever you need it. This answer shows how to handle XIB s.

This should work fine for your footer.

For more complex reusable views, which make sense to have their own UIViewConroller :

  • create the viewController in storyboard
  • instantiate it like this:

     let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "someViewController") 
  • add it as child viewController like this:

     func add(childViewController controller: UIViewController, embedViewIn containerView: UIView) { controller.willMove(toParentViewController: self) addChildViewController(controller) containerView.addSubview(controller.view) // addCustomConstraints } 

You can later remove the child viewController like this:

func remove(childViewController controller: UIViewController) {
    controller.willMove(toParentViewController: nil)
    controller.view.removeFromSuperview()
    controller.removeFromParentViewController()
}

您必须使用自定义tabBar,看看https://github.com/hartlco/MHCustomTabBarController这个链接,我已经实现了它很容易理解。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM