2012-07-12 131 views
0

我有一個圖像作爲子視圖的滾動視圖。我想將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; 

它就像我希望它(你看不到的背景),在工具欄上除頂部被圖像覆蓋。

回答

0

爲了解決這個問題,我最終宣佈新的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; 
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