简体   繁体   English

与UIScrollView结合使用时,UIPageControl不可见

[英]UIPageControl not visible when combined with UIScrollView

I'd like to make use of a page control to switch between several viewControllers. 我想使用页面控件在几个viewControllers之间切换。 I have the following viewController whose associated nib contains a UIScrollView and a UIPageControl. 我有以下viewController,其关联的nib包含UIScrollView和UIPageControl。 I've placed the scroll view above the page control by using Xcode's IB in a way that both controls are visible, and this is the .h file: 我已经通过使用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

The view and the scrollView outlet are linked to File's owner, as well as the scroll view's delegate. 视图scrollView插座链接到File的所有者,以及滚动视图的委托。 pageControl outlet and changePage are linked to the UIPageControl. pageControl outlet和changePage链接到UIPageControl。

This is the .m file (only relevant methods, actually): 这是.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;
}

When I run the app, what I see is that pages' views take up the whole screen, and I am able to navigate through pages by means of the scroll view's paging functionality, but the page control and its dots are not displayed. 当我运行应用程序时,我看到的是页面的视图占据整个屏幕,我能够通过滚动视图的分页功能浏览页面,但页面控件及其点不会显示。 What can I be missing? 我能错过什么?

Thanks! 谢谢!

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

"If the page control's and container's background color is the same (by default white) page control won't be visible." “如果页面控件和容器的背景颜色相同(默认为白色),则页面控件将不可见。”

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

将页面控制器放在ScidView的xid侧面,并为页面控制器提供适当的颜色。

I spent a few days trying to figure this out and just could not find any solutions on the web that solved it for me. 我花了几天时间试图解决这个问题,但是在网上找不到任何解决方案的解决方案。 Here's what hit me and actually worked for me- Your UIScroll View may be taking up the entire View Controller, therefor covering the Page Control. 这就是打击我并实际为我工作的东西 - 您的UIScroll View可能会占用整个View Controller,因此覆盖了页面控制。 Try simply shortening the Scroll View a little and your Page Control will obviously be visible now. 尝试简单地缩短滚动视图,现在显然可以看到您的页面控制。 Match up the background of the Page Control to the background of the View Controller for a seamless finish. 将页面控件的背景与View Controller的背景相匹配,以实现无缝完成。

    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])
}

Please try the above code Add this as a subview of View where scroll view is kept ie, PageControl and Scrollview should be subviews of same UIView 请尝试以上代码将此添加为View的子视图,其中保存滚动视图,即PageControl和Scrollview应该是相同UIView的子视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM