UIPageControl is not displayed in conjunction with UIScrollView

I would like to use the page control to switch between multiple viewControllers. I have the following viewController, the nib associated with it contains a UIScrollView and a UIPageControl. I placed the scroll over the page control using Xcode IB so that both controls are visible, and this is the .h file:

@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 output of scrollView is associated with the owner of the file, as well as the scroll delegate. pageControl outlet and changePage are associated with UIPageControl.

This is the .m file (only relevant methods):

 @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 launch the application, what I see is that pageviews occupy the entire screen, and I can navigate the pages using the scroll scroll function, but the page control and its points do not appear, What can I lose?

Thanks!

+4
source share
5 answers

Answered here: fooobar.com/questions/466486 / ...

"If the page control color and the background color of the container are the same (white by default), the page control will not be visible."

+3
source

This is either the color or the order of the objects in IB.

0
source

Place your page controller on the ScrollView side in xid and give the Apposite color a page slider.

0
source

I spent several days trying to figure it out, and simply could not find the solutions on the Internet that solved it for me. Here, what struck me and really worked for me - In the UIScroll View, the entire view controller can be involved, for which the page control is used. Try just shortening the scroll a little, and now your control over the pages will be visible. Match the Page Control background against the background of the view controller for seamless processing.

0
source
  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 code above. Add this as a subview of View as scroll. the representation is preserved, i.e. PageControl and Scrollview must be subviews of the same UIView

0
source

Source: https://habr.com/ru/post/1445569/


All Articles