ios 如何去除 UIPageViewController 的底部间隙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19935887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove the bottom gap of UIPageViewController
提问by vishal dharankar
I am using UIPageViewController to show images full screen, the UIViewController which is added to UIPageController as a sub view / child has the images being showed using ImageView. Problem is the images arent comming fullscreen, instead the pagecontrol view's donts are appearing at the bottom and that space is completely wasted. Please check the image attached.
我正在使用 UIPageViewController 全屏显示图像,作为子视图/子视图添加到 UIPageController 的 UIViewController 使用 ImageView 显示图像。问题是图像不是全屏显示,而是页面控件视图的不显示出现在底部,并且该空间完全浪费了。请检查所附的图像。
Here is the code
这是代码
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
NewsItemViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
Here NewsItemViewController is UIViewController showing images and some text and The MainViewController implements UIPageViewControllerDataSource protocol and necessary methods in MainViewController.
这里 NewsItemViewController 是显示图像和一些文本的 UIViewController,MainViewController 在 MainViewController 中实现 UIPageViewControllerDataSource 协议和必要的方法。
I believe there has to be a way to do show the things in full screen.
我相信必须有一种方法可以全屏显示内容。
*** Also the MainViewController is a part of a storyboard if that matters.
*** 如果重要的话,MainViewController 也是故事板的一部分。
回答by vishal dharankar
Finally got the solution myself I just hide the page control from UIViewPageController and then extended the size of the UIPageViewController to cover up the gap left due to absense of page control.
最后自己得到了解决方案,我只是从 UIViewPageController 隐藏了页面控件,然后扩展了 UIPageViewController 的大小以弥补由于缺少页面控件而留下的空白。
NSArray *subviews = self.pageController.view.subviews;
UIPageControl *thisControl = nil;
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
thisControl = (UIPageControl *)[subviews objectAtIndex:i];
}
}
thisControl.hidden = true;
self.pageController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+40);
回答by Michael J.
Curious. The docssay:
好奇的。该文件说:
If both of the methods in “Supporting a Page Indicator” are implemented and the page view controller's transition style is UIPageViewControllerTransitionStyleScroll, a page indicator is visible.
如果“支持页面指示器”中的两个方法都实现,并且页面视图控制器的过渡样式为 UIPageViewControllerTransitionStyleScroll,则页面指示器可见。
Those two methods are:
这两种方法是:
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
}
Are you implementing these two data source methods? If so, perhaps if you remove them you won't have to manually remove the page control (dots)? A quick test would be to change the
您是否正在实施这两种数据源方法?如果是这样,也许如果您删除它们,您就不必手动删除页面控件(点)?一个快速的测试是改变
UIPageViewControllerTransitionStyleScroll
to
到
UIPageViewControllerTransitionStylePageCurl
and see if the page control indicator dots go away. (After commenting out your hide method, of course.)
并查看页面控制指示器点是否消失。(当然,在注释掉你的 hide 方法之后。)
回答by g.revolution
UIPageViewController dots are only shown when you have implemented following method:
UIPageViewController 点仅在您实现以下方法时显示:
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController;
check in you code. and also returning back zero in this method will hide the dots (UIPageControl)
签入你的代码。并且在此方法中返回零将隐藏点(UIPageControl)
回答by ZaEeM ZaFaR
I am adding for swift 2.2 compatible code
我正在添加 swift 2.2 兼容代码
for view in self.view.subviews {
if view.isKindOfClass(UIScrollView) {
view.frame = UIScreen.mainScreen().bounds
} else if view.isKindOfClass(UIPageControl) {
view.backgroundColor = UIColor.clearColor()
}
}
This is the Swift 4 compatible solution, embedded in viewDidLayoutSubviews
这是 Swift 4 兼容的解决方案,嵌入在 viewDidLayoutSubviews
for view in self.view.subviews {
if view.isKind(of:UIScrollView.self) {
view.frame = UIScreen.main.bounds
} else if view.isKind(of:UIPageControl.self) {
view.backgroundColor = UIColor.clear
}
}
回答by deepax11
Swift version :). Return Zero for below implementation inside UIPageViewController subclass. func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { return 0 }
斯威夫特版:)。在 UIPageViewController 子类中为以下实现返回零。funcpresentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { return 0 }
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { return 0 }
回答by SMS
Set your pager controller as so
将您的寻呼机控制器设置为这样
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
And implement,this method should return any value greater than 1
并实现,此方法应返回任何大于 1 的值
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return 2;
}
Now the gap at the bottom space is removed and no page control shown :)
现在底部空间的间隙被删除,没有显示页面控件:)