0
我把UIScrollView的子視圖的高度設置爲768,而UIScrollView的contentSize是1024 * 768。但是當我向上滑動或向下滑動時,子視圖(UIImageView)會向上或向下滾動一點。我不知道爲什麼。UIScrollView表現得很奇怪
這裏是我的代碼
-(void)viewDidLoad
{
[super viewDidLoad];
// scrollView is an outlet of UIScrollView
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
imageCount=3;
for (i = 1; i <= imageCount; i++)
{
NSString *imageName = [NSString stringWithFormat:@"tu%d_s.png", i];
UIImage *image = [UIImage imageNamed:imageName];
if(!image)
{
NSLog(@"%@ is nil",imageName);
}
UIImageView *TempImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = TempImageView.frame;
rect.size.height = 768;
rect.size.width = 1024;
TempImageView.frame = rect;
TempImageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView addSubview:TempImageView];
NSLog(@"viewDidLoad TempImageView width %f,height %f,x %f,y %f",TempImageView.frame.size.width,TempImageView.frame.size.height,TempImageView.frame.origin.x,TempImageView.frame.origin.y);
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (1024);
}
NSLog(@"shang2 layoutScrollImages width %f,height %f,x %f,y %f",view.frame.size.width,view.frame.size.height,view.frame.origin.x,view.frame.origin.y);
}
// set the content size so it can be scrollable
[scrollView setContentSize:CGSizeMake((imageCount * 1024), 768)];
NSLog(@"shang2 content width %f,height %f,x %f,y %f",scrollView.contentSize.width,scrollView.contentSize.height,scrollView.frame.origin.x,scrollView.frame.origin.y);
}
這裏是Xcode的日誌
2013-04-03 20:50:23.939 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.952 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.995 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.995 animation[1464:c07] shang2 layoutScrollImages width 7.000000,height 5.000000,x 741.000000,y 1019.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 1024.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 2048.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 content width 3072.000000,height 768.000000,x 0.000000,y 0.000000
我在故事板中將狀態欄和頂部欄設置爲無。 –
@ nimingzhe2008你可能在storyboard中沒有任何設置,但是當你展示你的視圖控制器時,你實際上沒有展示它嗎?是那裏的scrollview?的狀態欄,如果這樣可以解釋你的20個像素可以滾動。 –
謝謝。狀態實際上仍然存在。當我刪除狀態欄時,一切正常。 –