我用pagecontrol实现了一个滚动视图。我有一些带有 SizeClass (wAny,hAny) 的 XIB。添加每个 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。