在滚动视图iOS中滚动时,视图框架不正确

时间:2014-12-28 13:44:27

标签: ios objective-c uiscrollview uipagecontrol size-classes

我用pagecontrol实现了一个scrollview。我有一些带有SizeClass的XIB(wAny,hAny)。当添加每个Viewcontroller的视图时,宽度为600 * 600但是当更改页面时,iphone 5的宽度为320 * 568。你能帮我纠正吗?提前谢谢。

这是我的代码:

#define CONTENT_VIEW_HEIGHT     495
#define CONTENT_VIEW_SPACING    0
#define SCROLL_VIEW_PADDING     0

#define FIRST_PAGE              0
#define LAST_PAGE               self.numberOfPages - 1


@interface MLKPageViewController ()

@property(nonatomic,strong) NSArray *contentVCs;
@property(nonatomic,assign) NSInteger numberOfPages;
@property(nonatomic,assign) BOOL pageControlUsed;

@end

@implementation MLKPageViewController

@synthesize contentVCs;
@synthesize numberOfPages;
@synthesize pageControlUsed;

- (id)initWithContentViewControllers:(NSArray *)aContentVCs
{
    if( self = [super initWithNibName:@"PageViewVC" bundle:nil] )
    {
        self.contentVCs = aContentVCs;
        self.numberOfPages = aContentVCs.count;
    }

    return self;
}

#pragma mark -
#pragma mark View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    mlkPageControl.numberOfPages = self.numberOfPages;
    mlkPageControl.currentPageIndicatorTintColor = [UIColor blackColor];

    [self setupContentViews];
}

#pragma mark -
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if( !pageControlUsed )
    {
        CGFloat pageWidth = contentScrollView.frame.size.width;
        int page = floor((contentScrollView.contentOffset.x - pageWidth / 2 ) / pageWidth) + 1;
        mlkPageControl.currentPage = page;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    pageControlUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageControlUsed = NO;
    [self changeContentPage:mlkPageControl];
}

#pragma mark -
#pragma mark Methods

- (void)setupContentViews
{
    contentScrollView.contentSize = CGSizeMake(( self.numberOfPages * ([UIScreen mainScreen].bounds.size.width - 2 * SCROLL_VIEW_PADDING) ) + ( self.numberOfPages + 1 ) * CONTENT_VIEW_SPACING, CONTENT_VIEW_HEIGHT) ;

    for( int i = 0; i < self.numberOfPages; i++ )
    {
        UIView *contentView = ((UIViewController *)[self.contentVCs objectAtIndex:i]).view;
        contentView.frame = CGRectMake( ((i+1) * CONTENT_VIEW_SPACING) + i * contentView.frame.size.width, SCROLL_VIEW_PADDING, contentView.frame.size.width, contentView.frame.size.height);

        [contentScrollView addSubview:contentView];
    }
}

#pragma mark
#pragma mark Actions

- (IBAction)changeContentPage:(id)sender
{
    CGRect pageRect;
    UIView *contentView = ((UIViewController *)[self.contentVCs objectAtIndex:mlkPageControl.currentPage]).view;
    NSInteger currentPage = mlkPageControl.currentPage;

    if( mlkPageControl.currentPage == FIRST_PAGE || mlkPageControl.currentPage == LAST_PAGE )
    {
        pageRect = CGRectMake( (currentPage * CONTENT_VIEW_SPACING) + currentPage *  contentView.frame.size.width, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
    }
    else
    {
        pageRect = CGRectMake( (currentPage * CONTENT_VIEW_SPACING) + currentPage *  contentView.frame.size.width - CONTENT_VIEW_SPACING, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
    }

    [contentScrollView scrollRectToVisible:pageRect animated:YES];
}

调用函数- (IBAction)changeContentPage:(id)sender时,大小为320 * 568,但在调用- (void)setupContentViews时,大小为600 * 600。

0 个答案:

没有答案