UIScrollView没有显示uilabel文本

时间:2016-06-29 09:44:47

标签: ios objective-c iphone uiscrollview uipagecontrol

我正在开发这个简单的应用程序。其中有一些文本显示在uiscrollview和pagecontrol应用程序中没有给出任何错误但它没有在scrollview中显示任何文本。这是我的代码。

in .h

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;

in .m

@interface dashboardViewController ()
@property (nonatomic, strong) NSArray *pageText;
@property (nonatomic, strong) NSMutableArray *pageViews;

- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;
@end

@implementation dashboardViewController
@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;

@synthesize pageText = _pageText;
@synthesize pageViews = _pageViews;
#pragma mark -

- (void)loadVisiblePages {
    // First, determine which page is currently visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));

    // Update the page control
    self.pageControl.currentPage = page;

    // Work out which pages you want to load
    NSInteger firstPage = page - 1;
    NSInteger lastPage = page + 1;

    // Purge anything before the first page
    for (NSInteger i=0; i<firstPage; i++) {
        [self purgePage:i];
    }
    for (NSInteger i=firstPage; i<=lastPage; i++) {
        [self loadPage:i];
    }
    for (NSInteger i=lastPage+1; i<self.pageText.count; i++) {
        [self purgePage:i];
    }
}

- (void)loadPage:(NSInteger)page {
    if (page < 0 || page >= self.pageText.count) {
        // If it's outside the range of what we have to display, then do nothing
        return;
    }

    // Load an individual page, first checking if you've already loaded it
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView == [NSNull null]) {
        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0.0f;
        frame = CGRectInset(frame, 10.0f, 0.0f);

        UILabel *newPageText = [[UILabel alloc] init];
        newPageText.textColor = [UIColor whiteColor];
        newPageText.contentMode = UIViewContentModeScaleAspectFit;
        newPageText.frame = frame;
        [self.scrollView addSubview:newPageText];
        [self.pageViews replaceObjectAtIndex:page withObject:newPageText];
    }
}

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageText.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    // Remove a page from the scroll view and reset the container array
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Load the pages that are now on screen
    [self loadVisiblePages];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {        
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // Set up the content size of the scroll view
    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageText.count, pagesScrollViewSize.height);
    // Load the initial set of pages that are on screen
    [self loadVisiblePages];
    [super viewWillAppear:animated];
}

这里是ViewDidLoad

- (void)viewDidLoad {

    self.pageText = @[@"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", @"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", @"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."];
    NSInteger pageCount = self.pageText.count;
    // Set up the page control
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;
    // Set up the array to hold the views for each page
    self.pageViews = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < pageCount; ++i) {
        [self.pageViews addObject:[NSNull null]];
    }
[super viewDidLoad];
    // Do any additional setup after loading the view.
}

这是截图 enter image description here

0 个答案:

没有答案