为什么我的UIpageControl没有显示?

时间:2012-06-14 03:06:00

标签: uiscrollview uipagecontrol

我下载了一个关于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)发送者;

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;
}

2 个答案:

答案 0 :(得分:1)

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

在IOS6中,您有新方法pageIndicatorTintColorcurrentPageIndicatorTintColor

希望这会有所帮助。

答案 1 :(得分:0)

检查容器视图/ tableView的顶部和底部约束(如果存在)。两者都不能与顶部/底部布局指南相关联。用容器边缘附上它们。