UIPageViewController页面指示器推送视图

时间:2013-09-18 18:47:41

标签: ios iphone objective-c ios7 uipageviewcontroller

我的页面视图控制器的页面指示器正在推动我的其余视图,而不是在它上面。如何将其分层放在视图上方?

编辑:我附上了正在发生的事情和代码的图片。

#import "RootViewController.h"
#import "PageViewController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"

@interface RootViewController () <UIPageViewControllerDataSource>

@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) AViewController *aViewController;
@property (strong, nonatomic) BViewController *bViewController;
@property (strong, nonatomic) CViewController *cViewController;

@end

@implementation RootViewController

@synthesize pageViewController;
@synthesize aViewController;
@synthesize bViewController;
@synthesize cViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
    if (iOSDeviceScreenSize.height == 480)
    {
        UIStoryboard *iPhoneStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        aViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:@"AViewController"];
        bViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:@"BViewController"];
        cViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:@"CViewController"];
    }
    if (iOSDeviceScreenSize.height == 568)
    {
        UIStoryboard *iPhone5Storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone5" bundle:nil];
        aViewController = [iPhone5Storyboard instantiateViewControllerWithIdentifier:@"AViewController"];
        bViewController = [iPhone5Storyboard instantiateViewControllerWithIdentifier:@"BViewController"];
        cViewController = [iPhone5Storyboard instantiateViewControllerWithIdentifier:@"CViewController"];
    }

    pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    pageViewController.dataSource = self;
    [[pageViewController view] setFrame:CGRectMake(0, 0, 320, iOSDeviceScreenSize.height)];

    AViewController *initialViewController = (BViewController *)[self viewControllerAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:pageViewController];
    [[self view] addSubview:[pageViewController view]];
    [pageViewController didMoveToParentViewController:self];
}

#pragma mark - PageViewController Data Source

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    PageViewController *viewController;
    if (index == 0)
    {
        viewController = aViewController;
    }
    else if (index == 1)
    {
        viewController = bViewController;
    }
    else
    {
        viewController = cViewController;
    }
    viewController.index = index;
    return viewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    NSUInteger index = [(PageViewController *)viewController index];
    if (index == 0)
        return nil;
    index--;
    return [self viewControllerAtIndex:index];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    NSUInteger index = [(PageViewController *)viewController index];
    index++;
    if (index == 3)
        return nil;
    return [self viewControllerAtIndex:index];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return 3;
}

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

@end

enter image description here

1 个答案:

答案 0 :(得分:0)

子类pageViewController和ovver覆盖viewDidLayoutSubviews - &gt;

SWIFT - &gt;

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        var pageControl: UIPageControl?
        var scrollView: UIScrollView?

        for subview in view.subviews {
            if subview is UIPageControl {
                pageControl = subview as? UIPageControl
            }

            if subview is UIScrollView {
                scrollView = subview as? UIScrollView
            }
        }

        if let scrollView = scrollView, let pageControl = pageControl {
            var frame = scrollView.frame
            frame.size.height = frame.height + pageControl.frame.height
            scrollView.frame = frame
            view.bringSubview(toFront: pageControl)
        }
    }

目标-C - &gt;

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIPageControl* pageControl;
    UIScrollView*  scrollView;

    for (id subview in self.view.subviews) {

        if ([subview isKindOfClass:[UIPageControl class]]) {
            pageControl = subview;
        } else if ([subview isKindOfClass:[UIScrollView class]]) {
            scrollView = subview;
        }
    }

    if (scrollView!= nil && pageControl !=nil) {
        CGRect frame = scrollView.frame;
        frame.size.height = frame.size.height + pageControl.frame.size.height;
        scrollView.frame = frame;
        [self.view bringSubviewToFront:pageControl];
    }
}