【发布时间】:2013-11-18 23:16:19
【问题描述】:
我创建了我的第一个 UIPageControl,但我看不到它。
我看到的关于同一问题的每个问题都谈到点的颜色和背景的颜色是否相同,这就是问题所在 - 这不是我的问题。
这是正在发生的事情: 1)我向我的视图添加了一个 UIPageControl,它还包含一个滚动视图。滚动视图通过几个不同的视图进行分页。 UIPageControl 使用自动布局添加到滚动视图的正下方。 2)我在视图加载后更新页面控件的当前页面,然后每次滚动视图滚动。 3) 我的视图背景是深灰色,UIPageControl 的点应该是白色作为默认颜色,但我看不到它们。但是,当我将 UIPageControl 的背景设置为清晰以外的颜色时,我可以看到控件的框架坐在那里,我只是看不到点。 4) 我检查以确保 UIPageControl currentPage 属性设置正确。
我一定是犯了一个愚蠢的错误,但我似乎找不到它。这是我的代码:
//创建UIPageControl
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:32/255.0f green:68/255.0f blue:90/255.0f alpha:1];
//...create self.scrollView and add it to the view
[self createArrayOfViews]; //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 = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
self.scrollView = onBoardScrollView;
[self.view addSubview:self.scrollView];
[self.view addConstraints:@[onBoardScrollViewWidth, onBoardScrollViewHeight, onBoardScrollViewCenterY, onBoardScrollViewCenterX]];
UIPageControl *onBoardImagePageControl = [[UIPageControl alloc]init];
onBoardImagePageControl.translatesAutoresizingMaskIntoConstraints = NO;
onBoardImagePageControl.backgroundColor = [UIColor clearColor];
NSLayoutConstraint *onBoardPageControlTop = [NSLayoutConstraint constraintWithItem:onBoardImagePageControl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:onBoardScrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
NSLayoutConstraint *onBoardPageControlCenterX = [NSLayoutConstraint constraintWithItem:onBoardImagePageControl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
CGSize sizeForPageControl = [onBoardImagePageControl sizeForNumberOfPages:self.onboardImages.count];
NSLayoutConstraint *onBoardPageControlWidth = [NSLayoutConstraint constraintWithItem:onBoardImagePageControl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:sizeForPageControl.width];
NSLayoutConstraint *onBoardPageControlHeight = [NSLayoutConstraint constraintWithItem:onBoardImagePageControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:sizeForPageControl.height];
self.pageControl = onBoardImagePageControl;
[self.view addSubview:self.pageControl];
[self.view addConstraints:@[onBoardPageControlTop, onBoardPageControlCenterX, onBoardPageControlHeight, onBoardPageControlWidth]];
//... add a few other UI elements
}
//使用自动布局创建框架后,加载可见页面
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.onboardImages.count, 250);
self.scrollView.delegate = self;
[self loadVisiblePages];
}
//加载可见页面的代码
- (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++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.onboardImages.count; i++) {
[self purgePage: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 = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = -20.0f; //adjust
//UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.onboardImages objectAtIndex:page]];
UIView *newPageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
UIView *currentView = [self.onboardImages objectAtIndex:page];
[newPageView addSubview:currentView];
currentView.center = newPageView.center;
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
//滚动视图委托方法继续加载可见页面
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
//从滚动视图中删除看不见的页面
- (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 = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
感谢任何帮助。
【问题讨论】:
标签: ios objective-c uipagecontrol