为啥我的 UIpageControl 不显示?

Posted

技术标签:

【中文标题】为啥我的 UIpageControl 不显示?【英文标题】:Why is my UIpageControl is not displayed?为什么我的 UIpageControl 不显示? 【发布时间】:2012-06-14 03:06:00 【问题描述】:

我下载了一个关于 UIScorllView 和 UIPageControl 的圆顶。为什么 UIPageControl 不显示? 这是代码,我是 Iphone 新手。任何帮助将不胜感激!

ScrollView.M 我把 ResultViewController 放到了 ScrollView 中。我想用 PageController 滚动 resultViewController。

- (void)loadScrollViewWithPage:(int)page

    if (page < 0)
        return;
    if (page >= pageNumber)
        return;


    ResultViewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null])
    
        controller = [[ResultViewController alloc] initWithPageNumber:page locations:existLocations];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    

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


- (void)viewDidLoad

    [super viewDidLoad];

    existLocations = [FileManagerUtil readPlistFileForDictionary:@"Locations" fileName:@"CloudCheckLocations.plist"];

    pageNumber = [existLocations count];
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < pageNumber; i++)
    
        [controllers addObject:[NSNull null]];
    
    self.viewControllers = controllers;
    [controllers release];

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];


    [self.navigationItem setTitle:@"NetWork condition"];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * pageNumber, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

    pageContronl.numberOfPages = pageNumber;
    pageContronl.currentPage = 0;

    [pageContronl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

    [pageContronl setBackgroundColor:[UIColor blackColor]];
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
    [scrollView addSubview:pageContronl];

    [self.view addSubview:scrollView];





- (void)scrollViewDidScroll:(UIScrollView *)sender


    if (pageControlUsed)
    

        return;
    

    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageContronl.currentPage = page;

    [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 = pageContronl.currentPage;

    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];

    pageControlUsed = YES;

ScrollView.h

@interface ScrollViewController : UIViewController<UIScrollViewDelegate>

    NSMutableArray *viewControllers;
    NSString *currectNetWork;
    NSString *flag;
    NSString *locationName;

    IBOutlet UIScrollView *scrollView;
    IBOutlet UIPageControl *pageContronl;
    BOOL pageControlUsed;
    int pageNumber;

    NSMutableDictionary *existLocations;


@property (nonatomic,retain) NSString *currectNetWork;
@property (nonatomic,retain) NSString *flag;
@property (nonatomic,retain) NSString *locationName;
@property (nonatomic,retain) UIPageControl * pageContronl;
@property (nonatomic,retain) UIScrollView * scrollView;
@property (nonatomic,retain) NSMutableArray *viewControllers;
@property (nonatomic,retain) NSMutableDictionary *existLocations;
(IBAction)changePage:(id)sender;

ResultViewControl.M 此方法将由 ScrollView.M 调用

- (id)initWithPageNumber:(int)page locations :(NSMutableDictionary *) locations

    titilArray = [[NSArray alloc] initWithObjects:@"Today", @"Past 7 Day",@"Past 30 Day",nil];

    if (self = [super initWithNibName:@"ResultViewController" bundle:nil])
    
        pageNumber = page;
        existLocations = locations;
    
    return self;

【问题讨论】:

【参考方案1】:

检查 pageControl 和父视图的背景颜色。如果两者颜色相同(默认为白色),页面控件将不会显示。

ios6 中,您有新方法 pageIndicatorTintColorcurrentPageIndicatorTintColor

希望这会有所帮助。

【讨论】:

【参考方案2】:

检查容器视图/ tableView(如果存在)的顶部和底部约束。两者都不得附有顶部/底部布局指南。用容器边距附加它们。

【讨论】:

以上是关于为啥我的 UIpageControl 不显示?的主要内容,如果未能解决你的问题,请参考以下文章

UIPageControl(页面控件)不显示在 UIPageViewController

UIPageControl 显示不正确的页面

崩溃:[UIPageControl] 页面越界。请求 0 但页面控件只有 0 个页面:为啥?

UIPageControl 消失

UIPageControl 不显示

Swift UIPageControl 不显示点