滚动视图不显示 UIView 和 UIImage
Posted
技术标签:
【中文标题】滚动视图不显示 UIView 和 UIImage【英文标题】:Scrollview Does not Show with UIView and UIImage 【发布时间】:2014-10-01 04:39:08 【问题描述】:我有:
-
UIView 包含一个 ImageView
在此 UIView 中添加 UIScrollView 以启用 Scroll(不起作用)
我有一个 AlertView 来弹出这个 UIView
我添加了 UIImageView 和 UIScrollView 作为 UIView 的子视图
每次我运行代码时,都没有滚动视图存在的迹象。我不知道为什么。
代码如下:
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
//create image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 500, 500)];
//create a scrollview to contain the uiview above
UIScrollView* scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[scrollview setDelegate:self];
[scrollview setBouncesZoom:YES];
scrollview.contentSize = CGSizeMake(500,500);
//create a view to show picture
UIView *demoView = [[UIView alloc] init];
if(gestureRecognizer.view == _firstImageView)
UIImage *image = [UIImage imageNamed:_photoDetailModel[1]];
[imageView setImage:image];
CGSize imageSize = imageView.image.size;
[demoView setFrame: CGRectMake(0, 0, 290, 200)];
// [demoView setFrame: CGRectMake(0, 0,imageSize.width, imageSize.height)];
else
UIImage *image = [UIImage imageNamed:_photoDetailModel[2]];
[imageView setImage:image];
CGSize imageSize = imageView.image.size;
[demoView setFrame: CGRectMake(0, 0, 290, 200)];
//add imageview
[demoView addSubview:imageView];
//add scollview
[demoView addSubview:scrollview];
//create a pop up view to contain the above uiview
Customios7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
// Add some custom content to the alert view
[alertView setContainerView: demoView];
// Modify the parameters
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close", nil]];
// You may use a Block, rather than a delegate.
// [alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex)
// NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, (int)[alertView tag]);
// [alertView close];
// ];
[alertView setUseMotionEffects:true];
// And launch the dialog
[alertView show];
【问题讨论】:
【参考方案1】:现在您正在将scrollview
添加到demoView
。应该是相反的。
//add scrollview
[scrollview addSubview:demoView];
如果您希望 scrollview
成为警报视图的 contentView,您还希望将其设置为警报视图,而不是 demoView
。
// Add some custom content to the alert view
[alertView setContainerView:scrollview];
您的最终视图层次结构应如下所示:
| AlertView
| --- ScrollView
| ------- DemoView
| ----------- ImageView
【讨论】:
你能告诉我在这种情况下如何启用缩放吗?我尝试:scrollView.delegate = alertView;然后添加这个滚动视图委托方法:但它不起作用。 除非您将警报视图子类化,否则您将希望将委托设置为视图控制器。然后设置最小和最大缩放比例。最后实现viewForZoomingInScrollView
方法,返回demoView
。
我尝试这样做,但我没有调用 viewForZoomingInScrollView,这是我在 github 上的代码:github.com/anthony1810/PhotoViewer。
返回demoView
而不是imageView
。
我只是打开它并更改为demoView,它仍然无法正常工作:(以上是关于滚动视图不显示 UIView 和 UIImage的主要内容,如果未能解决你的问题,请参考以下文章
flowcover 视图替代方案(使用 uiwebview 不使用 uiimage)
UIView(放置在滚动视图内)在以编程方式更改高度常量时不显示其子视图