在我的应用程序中,我想在UIScrollview
中显示图像。在这里,我没有得到scrollview的高度和宽度作为图像的高度和宽度。如何让scrollview(Hight,Width)到我的图像视图(高度和宽度)? 这是我现在试过的。如何在iOS中以编程方式在UIScrollView中添加UIImageVIew?
for (int i=0; i<imageArray.count; i++)
{
NSString *str = [imageArray objectAtIndex:i];
NSString *bannerImageURL = [NSString stringWithFormat:@"http:url/%@", str];
CGFloat myOrigin = i * self.view.frame.size.width;
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width,imgScrollView.frame.size.height)];
myImageView.contentMode = UIViewContentModeScaleAspectFit;
myImageView.clipsToBounds = YES;
myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerImageURL]]];
imgScrollView.contentSize = CGSizeMake(self.view.frame.size.width * imageArray.count, self.view.frame.size.height);
CGPoint scrollPoint = CGPointMake(0, 0);
[imgScrollView setContentOffset:scrollPoint animated:YES];
imgScrollView.delegate = self;
[imgScrollView addSubview:myImageView];
这是我的委托方法。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[imgScrollView setContentOffset: CGPointMake(imgScrollView.contentOffset.x,0)];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1;
NSLog(@"Scrolling - You are now on page %i",page);
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *) targetContentOffset
NS_AVAILABLE_IOS(5_0)
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1;
NSLog(@"Dragging - You are now on page %i",page);
}
我在代码中犯了什么错误? 请帮帮我。
你的问题似乎是误导。你在哪里调用for循环? –
在视图中加载方法。 –
尝试在viewWillAppear/viewDidAppear中调用它。在视图加载时框架尺寸还没有正确计算。 –