gpt4 book ai didi

ios - PageViewController 页面指示器向上推 View

转载 作者:可可西里 更新时间:2023-11-01 06:15:39 25 4
gpt4 key购买 nike

我的页面 View Controller 的页面指示器将 View 的其余部分向上推,而不是在其顶部。如何将其分层放置在 View 上方?

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

#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

最佳答案

子类 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)
}
}

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

关于ios - PageViewController 页面指示器向上推 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18880006/

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