UIPageViewController with static content

时间:2016-02-12 20:37:00

标签: ios swift uikit

I want to put some static content (buttons, views etc) on UIPageViewController. But I don't know how.

Any ideas?

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

Just create a container type="button" that will have e.g. a static button and an empty uiviewcontroller. Create them as iboutlets and a usual property uiview called pvc. Then you can create a pvc in code and add it to the uiview outlet via UIPageViewController.

This is some code copied from a project i did last weeek. There are Skidata in a addSubview: and put an UIPageViewController of a snowflake as overlay about it. I copied only the relevant parts of code. Hope this will help you:

SkiViewController.m

UIImage

SkiSubViewController.h

@interface SkiViewController () <UIPageViewControllerDataSource>

@property (strong, nonatomic) UIPageViewController *pageController;
@property (strong, nonatomic) SkiDataArray* skiDataArray;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIView *pageDataView;

@end

@implementation SkiViewController

- (SkiSubViewController *)viewControllerAtIndex:(NSUInteger)index
{
    SkiSubViewController *childViewController = [[SkiSubViewController alloc] initWithNibName:@"SkiSubViewController" bundle:nil];
    childViewController.indexNumber = index;

    if(self.skiDataArray)
    {
        childViewController.skiData = self.skiDataArray[index];
    }

    return childViewController;
}

#pragma mark - lify cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;

    [self addPageControllerViewControllers];
    [self addPageControllerView];

    [self addChildViewController:self.pageController];
    [self.pageController didMoveToParentViewController:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateData]; // download ski data and stuff
}


- (void)addPageControllerViewControllers
{
    SkiSubViewController *viewController = [self viewControllerAtIndex:0];

    if(self.skiDataArray)
    {
        viewController.skiData = self.skiDataArray[0];
    }

    [self.pageController setViewControllers:@[viewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (void)addPageControllerView
{
    self.pageController.view.translatesAutoresizingMaskIntoConstraints = NO; // avoids conflicts with auto generated constraints
    [self.pageDataView addSubview:self.pageController.view];

    NSDictionary *views = @{ @"subview": self.pageController.view };
    [self.pageDataView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview]|" options:0 metrics: 0 views:views]];
    [self.pageDataView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subview]|" options:0 metrics: 0 views:views]];
    [self.pageDataView updateConstraintsIfNeeded];
}

#pragma mark - uipageviewcontroller data source

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [(SkiSubViewController *)viewController indexNumber];

    if (index == 0)
    {
        return nil;
    }
    else
    {
        index--;
        return [self viewControllerAtIndex:index];
    }
}

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((SkiSubViewController *)viewController).indexNumber;
    index++;

    if (index == self.skiDataArray.count)
    {
        return nil;
    }
    else
    {
        return [self viewControllerAtIndex:index];
    }
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return self.skiDataArray.count;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

@end

SkiSubViewController.m

@interface SkiSubViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (assign, nonatomic) NSInteger indexNumber;
@property (strong, nonatomic) SkiData* skiData;


@end