4

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

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

#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

在此处输入图像描述

4

1 回答 1

1

子类 pageViewController 和 ovver 覆盖 viewDidLayoutSubviews ->

斯威夫特 - >

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 ->

- (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];
    }
}
于 2016-11-19T14:26:31.183 回答