PageViewController 页面指示器向上推视图

Posted

技术标签:

【中文标题】PageViewController 页面指示器向上推视图【英文标题】:PageViewController page indicator pushing view up 【发布时间】:2013-09-18 18:47:41 【问题描述】:

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

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

#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

【问题讨论】:

愿意向我们展示代码或告诉我们您在 IB 中做了什么让它做到这一点? 我没有在界面生成器中这样做 - 我使用根视图控制器以编程方式完成了这一切,然后实现了 pageviewcontrollerdatasource 协议。 好的,您可以在您的问题中发布相关代码吗? 当然。 1秒。我也发个图。 【参考方案1】:

子类 pageViewController 和 ovver 覆盖 viewDidLayoutSubviews ->

SWIFT->

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

【讨论】:

我可以使用这个版本。我将滚动视图框架设置为 self.view.frame。这填满了我想要的整个屏幕

以上是关于PageViewController 页面指示器向上推视图的主要内容,如果未能解决你的问题,请参考以下文章

页面指​​示器不同步 swift

如何在页面控件swift上捕获点击事件

修复 UIPageControl(不是滑动)

如何在 SwiftUI 中制作动态 PageViewController?

在 UIPageViewController 的 UIPageControl 中增加指示器的大小

在 UIPageViewController 的 UIPageControl 中增加指示器的大小