gpt4 book ai didi

iphone - UIpageviewcontroller 首次加载 View 时仅在横向模式下加载单个 View

转载 作者:行者123 更新时间:2023-12-03 20:14:24 25 4
gpt4 key购买 nike

我需要UIpageviewcontroller方面的帮助来实现通用应用程序。我在我的应用程序中添加了此功能,它工作正常,但是当我第一次在横向模式下加载 UIpageviewcontroller 时,它仅显示单个 View 。我的问题是,如果设备处于横向模式,它第一次只会加载单个 View 。我认为这是因为 UIpageviewcontroller 的声明代码。

代码-:

self.Pagecontrol = [[UIPageViewController alloc] initWithTransitionStyle:style
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];



self.Pagecontrol.delegate = self;

self.Pagecontrol.dataSource = self;


//Step 3:
//Set the initial view controllers.

PageCurlView *contentViewController = [[PageCurlView alloc] init];

NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
// &&& i add single view controller at time of declaration.

CGRect pageViewRect = CGRectMake(self.view.frame.origin.x+15, self.view.frame.origin.y+15, self.view.frame.size.width-45, self.view.frame.size.height-35);
pageViewRect = CGRectInset(pageViewRect, 5.0, 20.0);
self.Pagecontrol.view.frame = pageViewRect;


[self.Pagecontrol setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];


//Step 4:
//ViewController containment steps
//Add the pageViewController as the childViewController
[self addChildViewController:self.Pagecontrol];

//Add the view of the pageViewController to the current view


[self.BaseScrollView addSubview:self.Pagecontrol.view];
[self.view addSubview:self.BaseScrollView];

[self.Pagecontrol.view bringSubviewToFront:self.BaseScrollView];


[self.Pagecontrol didMoveToParentViewController:self];

//Step 5:
// set the pageViewController's frame as an inset rect.


//Step 6:
//Assign the gestureRecognizers property of our pageViewController to our view's gestureRecognizers property.

self.Pagecontrol.view.gestureRecognizers = self.Pagecontrol.gestureRecognizers;

//任何人都可以帮我解决这个问题。

感谢您提前提供帮助。

最佳答案

PVCViewController.h

#import <UIKit/UIKit.h>
#import "ContentViewController.h"


@interface PVCViewController : UIViewController<UIPageViewControllerDataSource>
{
UIPageViewController *pageController;
NSArray *pageContent;

}
@property (strong, nonatomic) UIPageViewController *pageController;
@property (strong, nonatomic) NSArray *pageContent;

@end

PVCViewController.m

#import "PVCViewController.h"

@interface PVCViewController ()

@end

@implementation PVCViewController
@synthesize pageController, pageContent;

- (void) createContentPages
{
NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
for (int i = 1; i < 11; i++)
{
NSString *contentString = [[NSString alloc]
initWithFormat:@"<html><head></head><body><h1>Chapter %d</h1><p>This is the page %d of content displayed using UIPageViewController in iOS 5.</p></body></html>", i, i];
[pageStrings addObject:contentString];
}

pageContent = [[NSArray alloc] initWithArray:pageStrings];

}

- (ContentViewController*)viewControllerAtIndex:(NSUInteger)index
{
// Return the data view controller for the given index.
if (([self.pageContent count] == 0) ||
(index >= [self.pageContent count])) {
return nil;
}

// Create a new view controller and pass suitable data.
ContentViewController *dataViewController =
[[ContentViewController alloc]initWithNibName:@"ContentViewController" bundle:nil];
dataViewController.dataObject =[self.pageContent objectAtIndex:index];
return dataViewController;
}

-(NSUInteger)indexOfViewController:(ContentViewController *)viewController
{
return [self.pageContent indexOfObject:viewController.dataObject];
}

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(ContentViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}

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

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(ContentViewController *)viewController];
if (index == NSNotFound) {
return nil;
}

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

- (void)viewDidLoad
{
[super viewDidLoad];
[self createContentPages];

NSDictionary *options =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];

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

self.pageController.doubleSided=YES;

pageController.dataSource = self;
[[pageController view] setFrame:[[self view] bounds]];


NSArray *viewControllers =[NSArray arrayWithObjects:[self viewControllerAtIndex:0],[self viewControllerAtIndex:1],nil];

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

[self addChildViewController:pageController];
[[self view] addSubview:[pageController view]];
[pageController didMoveToParentViewController:self];
// Do any additional setup after loading the view, typically from a nib.
}

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

@end

ContentViewController.h

#import <UIKit/UIKit.h>

@interface ContentViewController : UIViewController
{
UIPageViewController *pageController;
NSArray *pageContent;
}
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) id dataObject;
@end

ContentViewController.m

#import "ContentViewController.h"

@interface ContentViewController ()

@end

@implementation ContentViewController
@synthesize webView, dataObject;

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[webView loadHTMLString:dataObject
baseURL:[NSURL URLWithString:@""]];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

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

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

@end

将WebView添加到contentViewController xib并将outlet连接到ContentViewController.h文件中的webView。

就是这样..

这里是输出

enter image description here

关于iphone - UIpageviewcontroller 首次加载 View 时仅在横向模式下加载单个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16834837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com