我有一个带有图像作为子视图的滚动视图。我想将滚动视图的边界设置为图像视图的大小,这样您就看不到任何背景。
我不想再发生这种事了。
奇怪的是,在您放大或缩小图像后,边界似乎会自行固定,您无法再将图像移开并看到背景。
这就是我想要的代码:
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
// return which subview we want to zoom
return self.imageView;
}
-(void)viewDidLoad{
[super viewDidLoad];
[self sendLogMessage:@"Second View Controller Loaded"];
//sets the initial view to scale to fit the screen
self.imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
//sets the content size to be the size our our whole frame
self.scrollView.contentSize = self.imageView.image.size;
//setes the scrollview's delegate to itself
self.scrollView.delegate = self;
//sets the maximum zoom to 2.0, meaning that the picture can only become a maximum of twice as big
[self.scrollView setMaximumZoomScale : 2.5];
//sets the minimum zoom to 1.0 so that the scrollview can never be smaller than the image (no matter how far in/out we're zoomed)
[self.scrollView setMinimumZoomScale : 1.0];
[imageView addSubview:button];
}
我以为这条线会解决我的问题
//sets the content size to be the size our our whole frame
self.scrollView.contentSize = self.imageView.image.size;
但就像我说的,它只有在我放大或缩小后才有效。
编辑:当我切换
self.scrollView.contentSize = self.imageView.image.size;
到
self.scrollView.frame = self.imageView.frame;
它就像我想要的那样工作(你看不到背景),除了顶部的工具栏被图像覆盖。