设置视图边界

时间:2012-07-12 17:58:14

标签: objective-c uiview uiscrollview uiimageview

我有一个带有图像的滚动视图作为子视图。我想将scrollview的边界设置为图像视图的大小,这样您就无法看到任何背景。

enter image description here

我不想再发生这种事了。

奇怪的是,在放大或缩小图像后,边界似乎自行修复,您无法再将图像移开并看到背景。

这就是我的代码:

-(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;

它的工作方式与我想要的一样(你看不到背景),除了顶部的工具栏被图像覆盖。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

为了解决这个问题,我最终声明了一个新的CGRect,将其原点设置为我的scrollView的原点,使用我的视图边界设置其大小,然后将此CGRect分配回我的scrollview框架

CGRect scrollFrame;
scrollFrame.origin = self.scrollView.frame.origin;
scrollFrame.size = CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.scrollView.frame = scrollFrame;