Embed UIViewController Programmatically?

前端 未结 2 1725
盖世英雄少女心
盖世英雄少女心 2020-12-28 14:12

I have a Storyboard setup with a UIViewController with an container view so that I can embed another UIViewController inside of it.

In a ce

2条回答
  •  太阳男子
    2020-12-28 14:30

    Swift version with UIPageViewController (which I believe is a common use case for this)

    let pageController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    // do your PageViewController stuff here (delegate and dataSource)
    pageController.willMove(toParent: self)
    containerView.addSubview(pageController.view)
    pageController.view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
                pageController.view.topAnchor.constraint(equalTo: containerView.topAnchor),
                pageController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
                pageController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
                pageController.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor)
    ])
    addChild(pageController)
    pageController.didMove(toParent: self)
    

    I didn't figure out how to make use of the UIPageControll included in UIPageViewController so I ended up using my own instance. But I believe this is out of the scope of this question.

提交回复
热议问题