xcode 如何以编程方式为 UIPageControl 设置当前页面?

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

How to programmatically set the current page for UIPageControl?

iphonexcodeuiviewcontrolleruiscrollviewuipagecontrol

提问by RexOnRoids

I have 3 "pages" (page 0, page 1, page 2) in a UIScrollView that are snapped to by finger swipes. There is a UIPageControl there too. The UIScrollView starts off presenting page 0. What I want to do is present page 3 FIRST sometimes. How can I do this programmatically.

我在 UIScrollView 中有 3 个“页面”(第 0 页、第 1 页、第 2 页),它们通过手指滑动对齐。那里也有一个 UIPageControl。UIScrollView 开始呈现第 0 页。我想做的是有时首先呈现第 3 页。我怎样才能以编程方式做到这一点。

Simply setting currentPage (of UIPageControl) to the page number does nothing by the way.

顺便说一下,简单地将 currentPage(UIPageControl 的)设置为页码没有任何作用。

回答by Liam

(From the PageControl example)

(来自 PageControl 示例)

To go directly to a particular section of the scrollView use

要直接转到 scrollView 的特定部分,请使用

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];


I just implemented the same, e.g scrollview and page control and used the

我只是实现了相同的,例如滚动视图和页面控制并使用了

pageControl.currentPage = {pageNumber};

which worked perfectly, are you sure that the pageControl outlet is set correctly in Interface Builder (if you're using it).

效果很好,您确定在 Interface Builder 中正确设置了 pageControl 插座(如果您正在使用它)。

If the problem is that the scrollView is not working, and not the pageControl. Then make sure that you have the contentsize of the scrollView set correctly.

如果问题是 scrollView 不起作用,而不是 pageControl。然后确保您正确设置了 scrollView 的 contentsize 。

    scrollView.contentSize = CGSizeMake(320*numberOfPages, scrollView.frame.size.height);

To this scrollview you then add the content to each section

到此滚动视图,然后将内容添加到每个部分

page1ViewController.view.frame = CGRectMake(0, 0, 320, 200);
[scrollView addSubview:page1ViewController.view];

Next page gets pushed over 1 screen width

下一页被推送超过 1 个屏幕宽度

page2ViewController.view.frame = CGRectMake(320, 0, 320, 200);
[scrollView addSubview:page2ViewController.view];