OGeek|极客世界-中国程序员成长平台

标题: ios - UIPageControl 点不可见但框架可见 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:26
标题: ios - UIPageControl 点不可见但框架可见

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

我看到的关于同一问题的每个问题都在谈论点的颜色和背景的颜色是否相同,这就是问题所在 - 这不是我的问题。

这是正在发生的事情: 1)我向我的 View 添加了一个 UIPageControl,它还包含一个 ScrollView 。 ScrollView 通过几个不同的 View 进行分页。 UIPageControl 使用自动布局添加到 ScrollView 的正下方。 2)我在 View 加载后更新页面控件的当前页面,然后每次 ScrollView 滚动。 3) 我的 View 背景是深灰色,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 constraintWithItemnBoardImagePageControl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItemnBoardScrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
      NSLayoutConstraint *onBoardPageControlCenterX = [NSLayoutConstraint constraintWithItemnBoardImagePageControl 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 constraintWithItemnBoardImagePageControl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:sizeForPageControl.width];
      NSLayoutConstraint *onBoardPageControlHeight = [NSLayoutConstraint constraintWithItemnBoardImagePageControl 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)loadPageNSInteger)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];
      }
    }

// ScrollView 委托(delegate)方法继续加载可见页面

    - (void)scrollViewDidScrollUIScrollView *)scrollView {
      // Load the pages that are now on screen
      [self loadVisiblePages];
    }

//从 ScrollView 中删除看不见的页面

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

感谢任何帮助。



Best Answer-推荐答案


顺序似乎错误。您正在为被覆盖的对象设置 currentPage 和 pageCount。

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

关于ios - UIPageControl 点不可见但框架可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060025/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4