介绍具有模态演示风格的UIPageViewController

时间:2014-05-08 23:23:33

标签: ios objective-c uiscrollview storyboard uipageviewcontroller

目前我正在以模式方式显示一个视图控制器(嵌入在导航中),并以UIscrollView作为子视图。我想用UIPageViewController替换滚动视图,但我无法调整UIPageViewController的大小,也不能以模态方式显示它。解决方案,想法? 这是我现在的代码:

UIViewController *viewController = [[UIViewController alloc] init];
    UINavigationController *modalNav = [[UINavigationController alloc] initWithRootViewController:viewController];
    UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                             target:self
                                                                             action:@selector(dismissModalViewControllerAnimated:)];
    modalNav.navigationBar.topItem.leftBarButtonItem = dismiss;
    modalNav.modalPresentationStyle = UIModalPresentationFormSheet;

2 个答案:

答案 0 :(得分:0)

可以使用容器视图将视图控制器嵌入到另一个控件中。

删除滚动视图并将容器视图放在其位置。您将看到默认情况下将视图控制器嵌入其中。删除嵌入segue,将UIPageViewController拖放到故事板中并将其嵌入容器视图中。

看起来应该如下所示。

enter image description here

答案 1 :(得分:0)

更好的方法是创建一个viewcontroller,添加uipageviewcontrolller作为其子视图。

enter image description here

这是我的Gallery Container Page的代码。我对图库容器页面进行了推送,以避免导航问题(现在,PageViewController的父视图控制器是图库容器。)

import UIKit

class GalleryContainerPage: UIViewController,UIPageViewControllerDataSource {

private var pageViewController: UIPageViewController?
    private let contentImages = ["person-icon.jpg",
        "3807343799880.jpg","4701365629867.jpg","4713011921017.jpg"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBarHidden = false



        // Do any additional setup after loading the view.
    }


    private func createPageViewController() {

        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController

        pageController.dataSource = self

        if contentImages.count > 0 {
            let firstController = getItemController(0)!
            let startingViewControllers: NSArray = [firstController]
            pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }

        pageViewController = pageController
        addChildViewController(pageViewController!)

        pageViewController!.didMoveToParentViewController(self)
        self.view.addSubview(pageViewController!.view)

    }

private func setupPageControl() {

    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.darkGrayColor()
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemController

    if itemController.itemIndex > 0 {
        return getItemController(itemController.itemIndex-1)
    }

    return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemController

    if itemController.itemIndex+1 < contentImages.count {
        return getItemController(itemController.itemIndex+1)
    }

    return nil
}

private func getItemController(itemIndex: Int) -> PageItemController? {

    if itemIndex < contentImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as! PageItemController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = contentImages[itemIndex]
        return pageItemController
    }

    return nil
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {

    return contentImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillAppear(animated: Bool) {

        setupPageControl()
        createPageViewController()
    }

}