ios 从 UIPageViewController 隐藏点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20748897/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:51:52  来源:igfitidea点击:

Hide dots from UIPageViewController

ioscocoa-touchios7uipageviewcontrolleruipagecontrol

提问by Stefan Salatic

I would like to do to a pretty simple thing. Just remove all the dots, and the bar on the bottom of the UIPageViewController.

我想做一件很简单的事情。只需删除所有的点,以及 UIPageViewController 底部的栏。

This is the setup: I have a custom view controller which has UIPageViewController *pageController I display it like this:

这是设置:我有一个自定义视图控制器,它有 UIPageViewController *pageController 我像这样显示它:

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

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

BSItemPageViewController *initialViewController = [self viewControllerAtIndex:selectedIndex];

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

Any ideas on how do I remove the dots?

关于如何删除点的任何想法?

回答by jrturton

The page control is only displayed if the datasource implements these methods:

页面控件仅在数据源实现以下方法时才显示:

presentationCountForPageViewController:
presentationIndexForPageViewController:

Simply remove your implementation of these, and the page control will not be displayed. From the datasource docs:

只需删除您对这些的实现,页面控件将不会显示。从数据源文档

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,则页面指示器可见。

回答by mts

In my situation I have multiple UIPageViewControllers (created from -[UITableView didSelectRowAtIndexPath]), some of which contain only 1 page. Instead of using different controllers for different UITableView rows, I implemented the UIPageViewController delegate method as follows:

在我的情况下,我有多个 UIPageViewControllers(从 -[UITableView didSelectRowAtIndexPath] 创建),其中一些只包含 1 页。我没有为不同的 UITableView 行使用不同的控制器,而是实现了 UIPageViewController 委托方法,如下所示:

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return ([self numberOfPages] == 1 ? 0 : [self numberOfPages]);
}

This returns 0 if there is only 1 page which seems to make UIPageViewController not show the dots. It's a kludge but it appears to work (iOS SDK 7.0).

如果只有 1 个页面似乎使 UIPageViewController 不显示点,则返回 0。这是一个混乱,但它似乎工作(iOS SDK 7.0)。

I suppose a "cleaner" way would be to remove the methods at runtime for those UIPageControllers having only 1 page, but this would involve some fancy objC runtime manipulation.

我想一个“更干净”的方法是在运行时删除那些只有 1 个页面的 UIPageController 的方法,但这将涉及一些花哨的 objC 运行时操作。

Comments on this approach?

对此方法有何评论?

回答by iOSergey

If you want to hide those dots dynamically at runtime, then return -1 from the presentationIndexForPageViewController delegate:

如果你想在运行时动态隐藏这些点,那么从presentationIndexForPageViewController委托中返回-1:

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return yourPageVCs.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return yourPageVCs.count > 1 ? 0 : -1
}

In this case if you have only 1 (single sided) page, dots will be hidden.

在这种情况下,如果您只有 1 个(单面)页面,点将被隐藏。

回答by muhammed basil

The above fixes works fine with fixed number of pages at the beginning.

上述修复在开始时使用固定数量的页面可以正常工作。

I tried different approaches to solve the problem when pages can be increased or decreased dynamically.

当页面可以动态增加或减少时,我尝试了不同的方法来解决这个问题。

So I used below method which will manually hide the component itself.

所以我使用了下面的方法,它将手动隐藏组件本身。

func togglePageControl(pageCount: Int, threshold: Int = 1) {

    var hidden = true

    if pageCount > threshold {

        hidden = false

    }

    for subView in self.view.subviews {
        if subView is UIScrollView {
            subView.frame = self.view.bounds
        } else if subView is UIPageControl {
            subView.isHidden = hidden
        }
    }
}

And this should be called from

这应该从

public func presentationCount(for pageViewController: UIPageViewController) -> Int {

    togglePageControl(pageCount: pages.count)

    // or togglePageControl(pageCount: pages.count, threshold: 5)

    return pages.count
}

回答by Daniel

As Lee pointed out, simply not implementing the methods presentationCountForPageViewControllerand presentationCountForPageViewControllersuffers from an accessibility issue during voiceover where it won't read out the page number (1 of 5, 2 of 5, etc.). A solution that will support accessibility is:

正如 Lee 指出的那样,简单地不实现presentationCountForPageViewControllerpresentationCountForPageViewController方法在画外音期间遇到可访问性问题,它不会读出页码(1 of 5、2 of 5 等)。支持可访问性的解决方案是:

// Swift
let proxy = UIPageControl.appearance()
proxy.isHidden = true

// Objective-C
UIPageControl *proxy = [UIPageControl appearance];
[proxy setHidden:YES];

This has the added benefit of maintaining a cleaner separation between the data source and its presentation.

这有一个额外的好处,即在数据源和它的表示之间保持更清晰的分离。

UPDATE: This is unfortunately not a perfect solution since it hides the control but doesn't remove it (there remains an empty space the same height of the hidden control). I haven't yet been able to find a way to configure the existing control to fix this issue.

更新:不幸的是,这不是一个完美的解决方案,因为它隐藏了控件但没有删除它(仍然有一个与隐藏控件高度相同的空白空间)。我还没有找到一种方法来配置现有控件来解决这个问题。