【问题标题】:UIPageControl not visible when combined with UIScrollViewUIPageControl 与 UIScrollView 结合时不可见
【发布时间】:2012-11-01 02:50:54
【问题描述】:

我想利用一个页面控件在几个视图控制器之间切换。我有以下 viewController,其关联的 nib 包含 UIScrollView 和 UIPageControl。我使用 Xcode 的 IB 将滚动视图放置在页面控件上方,使两个控件都可见,这是 .h 文件:

@interface NewForm : UIViewController <UIScrollViewDelegate>
{
   BOOL pageControlUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;

- (IBAction)changePage:(id)sender;

@end

viewscrollView 出口链接到文件的所有者,以及滚动视图的委托。 pageControl outlet 和 changePage 链接到 UIPageControl。

这是 .m 文件(实际上只有相关的方法):

@implementation STNewAccountTest
@synthesize scrollView, viewControllers, pageControl;

- (void)viewDidLoad
{
  [super viewDidLoad];

  NSMutableArray *controllers = [[NSMutableArray alloc] init];
 [controllers addObject:[[Page1 alloc] initWithNibName:@"Page1" bundle:nil]];
 [controllers addObject:[[Page2 alloc] initWithNibName:@"Page2" bundle:nil]];
 self.viewControllers = controllers;

 scrollView.pagingEnabled = YES;
 scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
 scrollView.showsHorizontalScrollIndicator = NO;
 scrollView.showsVerticalScrollIndicator = NO;
 scrollView.scrollsToTop = NO;
 scrollView.delegate = self;

 self.pageControl.currentPage = 0;
 self.pageControl.numberOfPages = numberOfPages;

 [self loadScrollViewWithPage:0];
}

- (void)loadScrollViewWithPage:(int)page
{
  if ((page < 0) || (page >= numberOfPages))
    return;

  Page1 *controller1 = nil;
  Page2 *controller2 = nil;

  if (page == 0) {
    controller = [self.viewControllers objectAtIndex:page];

    if (controller == nil) {
        controller = [[Page1 alloc] initWithNibName:@"Page1" bundle:nil];
        [self.viewControllers replaceObjectAtIndex:page withObject:controller];
    }
}
if (page == 1) {
    controller = [self.viewControllers objectAtIndex:page];

    if (controller == nil) {
        controller = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil];
        [self.viewControllers replaceObjectAtIndex:page withObject:controller];
    }
}

if (controller.view.superview == nil)
{
    CGRect frame = self.scrollView.frame;
     frame.origin.x = frame.size.width * page;
     frame.origin.y = 0;
     controller.view.frame = frame;
    [self.scrollView addSubview:controller.view];
}
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
  if (pageControlUsed)
  {
    return;
  }

  // Switch the indicator when more than 50% of the previous/next page is visible
  CGFloat pageWidth = scrollView.frame.size.width;
  int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  pageControl.currentPage = page;

  // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
  [self loadScrollViewWithPage:page - 1];
  [self loadScrollViewWithPage:page];
  [self loadScrollViewWithPage:page + 1];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  pageControlUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  pageControlUsed = NO;
}

- (IBAction)changePage:(id)sender
{
  int page = pageControl.currentPage;

  // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
  [self loadScrollViewWithPage:page - 1];
  [self loadScrollViewWithPage:page];
  [self loadScrollViewWithPage:page + 1];

// update the scroll view to the appropriate page
  CGRect frame = scrollView.frame;
  frame.origin.x = frame.size.width * page;
  frame.origin.y = 0;
  [scrollView scrollRectToVisible:frame animated:YES];

// Set the boolean used when scrolls originate from the UIPageControl
  pageControlUsed = YES;
}

当我运行应用程序时,我看到页面的视图占据了整个屏幕,我可以通过滚动视图的分页功能在页面中导航,但是页面控件及其点不显示.我会错过什么?

谢谢!

【问题讨论】:

    标签: ios uiscrollview viewcontroller uipagecontrol uiscrollviewdelegate


    【解决方案1】:

    在这里回答:https://stackoverflow.com/a/4245642/1455770

    “如果页面控件和容器的背景颜色相同(默认为白色)页面控件将不可见。”

    【讨论】:

      【解决方案2】:

      是IB中对象的颜色或顺序。

      【讨论】:

        【解决方案3】:

        将您的页面控制器放在 xid 中的 ScrollView 之外,并为页面控制器提供合适的颜色。

        【讨论】:

          【解决方案4】:

          我花了几天时间试图解决这个问题,但在网上找不到任何可以为我解决问题的解决方案。这就是打击我并实际上对我有用的东西- 您的 UIScroll View 可能会占用整个 View Controller,因此会覆盖 Page Control。试着简单地缩短滚动视图,你的页面控件现在显然是可见的。 将 Page Control 的背景与 View Controller 的背景相匹配,以实现无缝完成。

          【讨论】:

            【解决方案5】:
                private func setupPageControl() {
            
                 pageControl = UIPageControl(frame:.zero)
                 pageControl.translatesAutoresizingMaskIntoConstraints = false
                 pageControl.currentPageIndicatorTintColor = UIColor.orange
                 pageControl.pageIndicatorTintColor = UIColor.lightGray.withAlphaComponent(0.8)
                 pageControl.numberOfPages = 3;
                 pageControl.currentPage = 0;
                 self.bringSubviewToFront(pageControl)
                 addSubview(pageControl)
            
                let widthConstraint = NSLayoutConstraint(item: pageControl, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 50)
                let heightConstraint = NSLayoutConstraint(item: pageControl, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 50)
                let horizontalConstraint = NSLayoutConstraint(item: pageControl, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
                let verticalConstraint = NSLayoutConstraint(item: pageControl, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
                self.addConstraints([widthConstraint, heightConstraint, horizontalConstraint,verticalConstraint])
            }
            

            请尝试上面的代码将其添加为 View 的子视图,其中滚动 视图被保留,即 PageControl 和 Scrollview 应该是 相同的 UIView

            【讨论】:

              猜你喜欢
              • 2017-05-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-04-01
              • 1970-01-01
              • 2021-01-09
              • 2019-01-11
              • 1970-01-01
              相关资源
              最近更新 更多