4

我想利用页面控件在几个视图控制器之间切换。我有以下 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

视图scrollView出口链接到 File的所有者,以及滚动视图的委托。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;
}

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

谢谢!

4

5 回答 5

3

Answered here: 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."

于 2013-02-12T13:39:24.987 回答
0

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

于 2013-09-12T07:06:31.680 回答
0

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

于 2013-03-13T08:31:04.487 回答
0

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

于 2015-09-25T19:55:49.280 回答
0
    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 的子视图

于 2019-08-20T07:30:23.007 回答