设置视图边界
问题描述:
我有一个图像作为子视图的滚动视图。我想将scrollview的边界设置为图像视图的大小,以便您无法看到任何背景。设置视图边界
我不希望这种情况发生了。
奇怪的是,在你放大或缩小图像后,边界似乎会自行修复,而且你不能再移动图像并看到背景。
这就是我要去下面的代码:
-(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;
它就像我希望它(你看不到的背景),在工具栏上除顶部被图像覆盖。
答
为了解决这个问题,我最终宣布新的CGRect
,将其原点设置为我的scrollView的原点,将其大小设置为我的视图的边界,然后将此CGRect分配回我的滚动视图框架
CGRect scrollFrame;
scrollFrame.origin = self.scrollView.frame.origin;
scrollFrame.size = CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.scrollView.frame = scrollFrame;
答
imageView.image.size不一定是ImageView的本身的框架,尝试设置 scrollview.frame = imageView.frame
然后
scrollView.contentSize = imageView.image.size
然后,你将不会看到任何边界。如果你想要的形象是开始使用的最大尺寸, 做
imageView.frame = image.size;
[imageView setImage:image];
scrollView.frame = self.view.frame; //or desired size
[scrollView addSubView:imageView];
[scrollView setContentSize:image.size]; //or imageView.frame.size