UIPageControl点不可见,但框架是可见的

时间:2013-11-18 23:16:19

标签: ios objective-c uipagecontrol

我创建了我的第一个UIPageControl但我看不到它。

我在同一个问题上看到的每一个问题都谈到了点的颜色和背景的颜色是一样的,这就是问题 - 这不是我的问题。

以下是发生的事情: 1)我在我的视图中添加了一个UIPageControl,它还包含一个滚动视图。滚动视图通过几个不同的视图分页。 UIPageControl使用autolayout添加到滚动视图的正下方。 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

    }

//使用autolayout创建帧后,加载可见页面

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

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

订单似乎错了。您正在为被覆盖的对象设置当前页面和页面计数。

  //Set here
  self.pageControl.currentPage = 0;
  self.pageControl.numberOfPages = pageCount;

  UIPageControl *onBoardImagePageControl = [[UIPageControl alloc]init];
  onBoardImagePageControl.translatesAutoresizingMaskIntoConstraints = NO;
  onBoardImagePageControl.backgroundColor = [UIColor clearColor];

  //Overwritten here
  self.pageControl = onBoardImagePageControl;

  [self.view addSubview:self.pageControl];