ios - UIPageControl 点不可见但框架可见
<p><p>我创建了我的第一个 UIPageControl,但我看不到它。</p>
<p>我看到的关于同一问题的每个问题都在谈论点的颜色和背景的颜色是否相同,这就是问题所在 - 这不是我的问题。</p>
<p>这是正在发生的事情:
1)我向我的 View 添加了一个 UIPageControl,它还包含一个 ScrollView 。 ScrollView 通过几个不同的 View 进行分页。 UIPageControl 使用自动布局添加到 ScrollView 的正下方。
2)我在 View 加载后更新页面控件的当前页面,然后每次 ScrollView 滚动。
3) 我的 View 背景是深灰色,UIPageControl 的点应该是白色作为默认颜色,但我看不到它们。但是,<strong>当我将 UIPageControl 的背景设置为非透明颜色时,我可以看到控件的框架坐在那里</strong>,我只是看不到点。
4) 我检查并确保 UIPageControl currentPage 属性设置正确。</p>
<p>我一定是犯了一个愚蠢的错误,但我似乎找不到它。这是我的代码:</p>
<p>//创建UIPageControl</p>
<pre><code> - (void)viewDidLoad
{
;
self.view.backgroundColor = ;
//...create self.scrollView and add it to the view
; //this is what makes up the pages in the scroll view
NSInteger pageCount = self.onboardImages.count; //this array is set in create array of views method above
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageViews = [ init];
for (NSInteger i = 0; i < pageCount; ++i) {
];
}
self.scrollView = onBoardScrollView;
;
];
UIPageControl *onBoardImagePageControl = [init];
onBoardImagePageControl.translatesAutoresizingMaskIntoConstraints = NO;
onBoardImagePageControl.backgroundColor = ;
NSLayoutConstraint *onBoardPageControlTop = ;
NSLayoutConstraint *onBoardPageControlCenterX = ;
CGSize sizeForPageControl = ;
NSLayoutConstraint *onBoardPageControlWidth = ;
NSLayoutConstraint *onBoardPageControlHeight = ;
self.pageControl = onBoardImagePageControl;
;
];
//... add a few other UI elements
}
</code></pre>
<p>//使用自动布局创建框架后,加载可见页面</p>
<pre><code> - (void)viewDidLayoutSubviews
{
;
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.onboardImages.count, 250);
self.scrollView.delegate = self;
;
}
</code></pre>
<p>//加载可见页面的代码</p>
<pre><code> - (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
;
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
;
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.onboardImages.count; i++) {
;
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.onboardImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
UIView *pageView = ;
if ((NSNull*)pageView == ) {
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = -20.0f; //adjust
//UIImageView *newPageView = [ initWithImage:];
UIView *newPageView = [initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
UIView *currentView = ;
;
currentView.center = newPageView.center;
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
;
;
}
}
</code></pre>
<p>// ScrollView 委托(delegate)方法继续加载可见页面</p>
<pre><code> - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
;
}
</code></pre>
<p>//从 ScrollView 中删除看不见的页面</p>
<pre><code> - (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.onboardImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = ;
if ((NSNull*)pageView != ) {
;
];
}
}
</code></pre>
<p>感谢任何帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>顺序似乎错误。您正在为被覆盖的对象设置 currentPage 和 pageCount。</p>
<pre><code>//Set here
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
UIPageControl *onBoardImagePageControl = [init];
onBoardImagePageControl.translatesAutoresizingMaskIntoConstraints = NO;
onBoardImagePageControl.backgroundColor = ;
//Overwritten here
self.pageControl = onBoardImagePageControl;
;
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIPageControl 点不可见但框架可见,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20060025/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20060025/
</a>
</p>
页:
[1]