ODENZA

We will show you how to ignore safe area when using UIScrollView.

Problem:

When we use UIScrollView, It can’t ignore safe area by default. or even you set the constraints of scrollView to the superView.

Solved:

Set content inset by programmatically.

yourScrollView.contentInsetAdjustmentBehavior = .never

or set it in your storyboard

--

--

In this article, I would like to show you how to create extension for add your childView.

protocol Containable {
func add(childView: UIViewController, container: UIView)
func remove(childView: UIViewController)
}
extension Containable where Self: UIViewController {
func add(
childView: UIViewController,
container: UIView) {
addChild(childView)
container.addSubview(childView.view)
childView.view.frame = container.bounds
childView.didMove(toParent: self)
}
func remove(childView: UIViewController) { childView.willMove(toParent: nil)
childView.view.removeFromSuperview()
childView.removeFromParent()
}
}

How to use it.

class ViewController: UIViewController, Containable {   @IBOutlet weak var yourView: UIView!
let yourViewController = YourViewController(nibName: "YourViewController", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
add(childView: yourViewController, container: yourView)
}
}

--

--