Adding a UIPageViewController to a subview, subview only shows edge of pageContentview

别来无恙 提交于 2020-01-24 07:50:07

问题


I'm trying to display an entire pageContentView within a SubView on a rootViewController. I'm able to see the pageView but it doesn't auto-resize correctly to fit the SubView, it only shows a cutoff portion of the pageContentView

Here's what it looks like

Here's what it should look like

I can't figure out how to resize the pageView so it display correctly in a subView.

RootViewController.h

#import <UIKit/UIKit.h>
#import "PageContentViewController.h"
@class PageContentViewController;

@interface CLDViewController : UIViewController <UIPageViewControllerDataSource>

@property (weak, nonatomic) IBOutlet UIView *subViewRoot;
- (IBAction)startWalkthrough:(id)sender;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *pageTitles;
@property (strong, nonatomic) NSArray *pageImages;

@property (strong,nonatomic) PageContentViewController *bounds;
@end

ViewDidLoad and required methods for UIPageViewController in RootController

   - (void)viewDidLoad
 {

    [super viewDidLoad];

    _pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"];
    _pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    //Trying to setup the pageViewContoller view to be inside the subview (subViewRoot)
    self.pageViewController.view.frame = self.subViewRoot.bounds;
    [self addChildViewController:_pageViewController];
    [self.subViewRoot addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];


}


    - (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.titleText = self.pageTitles[index];
    pageContentViewController.pageIndex = index;

    return pageContentViewController;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Page View Controller Data Source



- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

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

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

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

PageContentViewController.m

-viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.backgroundImageView.image = [UIImage imageNamed:self.imageFile];
    self.titleLabl.text = self.titleText;

}

Has anyone ran into this situation before? What did you do?

Thanks for the help!


回答1:


Since you are setting the frame of your UIPageViewController to the frame of that 'subview' placeholder view, you need to make sure that the frame for the placeholder will be accurate by when the view is loaded. ViewControllers loaded from xibs don't generally have a useful frame until their view is added somewhere, and the same goes for their subviews. viewWillAppear is usually the soonest you can be sure your viewController's view to be set correctly.

Disabling auto-layout will work, but auto layout can be a nice feature once you learn how to use it. In this case, you can add constraints from subViewRoot to its parent view - Leading, Trailing, Top and Bottom. Control dragging from the subViewRoot to the parent is an easy shortcut.




回答2:


I was able to get my view laid out as I wanted using constraints on the PageContentViewController and also adding constraints to the SubView on the RootViewController. (As Acey suggested)

I did this by selecting "resolve auto-layout issues" --> Reset to suggested auto layout constraints.

Then also ctr-clking inside the view and setting width and height constraints. I can even change the orientation and the layout is what I expect.

for anyone struggling with this feel free to send a pm or comment here.



来源:https://stackoverflow.com/questions/25695751/adding-a-uipageviewcontroller-to-a-subview-subview-only-shows-edge-of-pageconte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!