iOS Development about Addchildviewcontroller understanding
Objective
I was doing Android development before the contact iOS development less than one months of time, so in some east understanding will unconsciously toward the android direction.
Understand
Finding Addchildviewcontroller is similar to the Fragment we have in contact with Android by looking at the documentation and related blogs.
Benefits
We all know that the IOS development default mode is the MVC pattern, but there are still some MVVM MVP and so on, but these patterns are extended from MVC, MVC is their foundation.
For example, if we need to add a more complex view to the interface and this view involves other business logic processing. If we add the view directly to the A interface, you have two options for dealing with the view's interactive logic.
- A Interface handles business logic
- Working with business logic in View
The drawbacks of both scenarios are obvious, and the first one will cause the Controller to be very bloated and not conducive to later developer viewing. The second does not reduce the logic of the Controller, but it is not appropriate to write the business logic into the View, which conforms to the MVC pattern. So addchildviewcontroller this method can solve the problem perfectly, you just need to build a new controller, and then write the business logic into the new controller.
Fragment also has a similar effect in Android development, and even the entire App is done with Fragment, and Activity is just a carrier.
Usage
Addchildviewcontroller: Is a method of Uiviewcontroller, it is very simple to use.
Addchildviewcontroller and Addsubview appear simultaneously
let childVc = OtherViewController() addChildViewController(childVc) self.view.addSubview(childVc.view)
Set the location of a child view
childVc.view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
Remove Childcontroller
childVc.removeFromParentViewController()
View Toggle (If you have added 2 childcontroller at a time)
/** * from 显示在父视图控制器中的子视图控制器 * to 将要显示的姿势图控制器 * duration 动画时间 * options 动画效果(渐变,从下往上等等,具体查看API) * animations 转换过程中得动画 * completion 转换完成闭包 */ self.transition(from: FirstViewController(), to: SecondViewController(), duration: 5, options: UIViewAnimationOptions.curveEaseInOut, animations: nil, completion: nil)
Many of these parameters can be adjusted under the test, there are many animations, of course, you can also implement their own animation.
Summarize
Actually see here you can go very simple to achieve a similar to NetEase news that kind of tab view switch function, similar to our Android inside Viewpager inside nested fragmengt implementation method same. We can use Uicollectionview nested childviewcontrller to achieve similar effects.
iOS Development about Addchildviewcontroller understanding