IOS:在滚动视图中添加一些子滚动视图,图像不出现

Posted

技术标签:

【中文标题】IOS:在滚动视图中添加一些子滚动视图,图像不出现【英文标题】:IOS: add some sub-scrollview in a scrollview, image don't appear 【发布时间】:2012-01-09 11:16:54 【问题描述】:

我有这个代码:

- (void)viewDidLoad
 
[super viewDidLoad];

NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"], nil];

for (int i = 0; i < image.count; i++) 
    CGRect frame;
    frame.origin.x = scrollV.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = scrollV.frame.size;

    UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];

    [subview setImage:[image objectAtIndex:i]];
    [subScrollView addSubview:subview];
    [scrollV addSubview:subScrollView];
    [subview release];
    [subScrollView release];


scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);


scrollV 是主滚动视图,它具有“分页启用” 如果我使用此代码,我的 scrollV 启用了分页,但我在第一页看到里面只有“1.png”,我看不到其他 png,为什么?

【问题讨论】:

【参考方案1】:

您将subview 的框架设置为不正确的值。这使它与subScrollView 具有相同的框架,因此将其推向右侧。试试这个:

- (void)viewDidLoad
 
[super viewDidLoad];

NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"], nil];

for (int i = 0; i < image.count; i++) 
    CGRect frame;
    frame.origin.x = scrollV.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = scrollV.frame.size;

    UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
    UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];

    UIImage *image = [image objectAtIndex:i];
    subScrollView.contentSize = image.size;

    [subview setImage:image];
    [subScrollView addSubview:subview];
    [scrollV addSubview:subScrollView];
    [subview release];
    [subScrollView release];


scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);


编辑:也可能最好设置内部滚动视图的内容大小。我在上面添加了代码。

【讨论】:

以上是关于IOS:在滚动视图中添加一些子滚动视图,图像不出现的主要内容,如果未能解决你的问题,请参考以下文章

iOS UIScrollView捏缩放添加空白

从 URL 下载图像并添加到滚动视图子视图

在作为子视图添加的滚动视图中将触摸事件传递给 uiimageview

Xamarin iOS:滚动视图中的自动布局?

IOS:在滚动视图中添加图像视图以进行缩放

UIimage没有出现在iOS中的滚动视图上