2014-01-13 1 views
0

UIImage을 가진 UIImageView을 보유하는 UIScrollView을 설정했습니다. 이미지 뷰에로드하는 UIImage는 크기가 약 2300x1200이고 확대 할 수있는만큼 축소되지 않습니다.IOS에서 오리엔테이션 변경 후 UIImageView를 보유하고있는 UIScrollView의 레이아웃이 잘못되었습니다

UIScrollView에는 탭/두 번 탭을 사용하여 이미지를 확대 및 축소 할 수 있도록 설정된 UIImageView가 있습니다. 나는 followed a tutorial here to create this입니다.

예제의 소스 코드는 downloaded here. 일 수 있습니다.

문제
문제점은 방향 변경 때문입니다. 뷰가 더 이상 예상대로 배치되지 않고 오프셋됩니다. 이미지가 (내 예제에서) 다운로드되면

I 완료 다음

[self.mainImageView setImage:image]; 
self.mainImageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; 

// Tell the scroll view the size of the contents 
self.mainScrollView.contentSize = image.size; 

[self setupScales]; 

그때 다음과 setupScales 같이

// Set up the minimum & maximum zoom scales 
CGRect scrollViewFrame = self.mainScrollView.frame; 
CGFloat scaleWidth = scrollViewFrame.size.width/self.mainScrollView.contentSize.width; 
CGFloat scaleHeight = scrollViewFrame.size.height/self.mainScrollView.contentSize.height; 
CGFloat minScale = MIN(scaleWidth, scaleHeight); 

self.mainScrollView.minimumZoomScale = minScale; 
self.mainScrollView.maximumZoomScale = 1.0f; 
self.mainScrollView.zoomScale = minScale; 

[self centerScrollViewContents]; 

그때 다음과 centerScrollViewContents 같이

CGSize boundsSize = self.mainScrollView.bounds.size; 
CGRect contentsFrame = self.mainImageView.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
} else { 
    contentsFrame.origin.x = 0.0f; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
} else { 
    contentsFrame.origin.y = 0.0f; 
} 

self.mainImageView.frame = contentsFrame; 

scrollView 및 imageView는 다음과 같이 설정됩니다.

scrollView에는 왼쪽/오른쪽/위/아래에 대한 제약 조건이 0으로 고정되어 있으며 imageView에는없는 제약 조건이 없습니다. willRotateToInterfaceOrientation에서 self.mainScrollView needsUpdateConstraints을 실행하려고 시도했지만 차이가 없습니다.

첫 번째 옵션 '이미지 확대/축소'를 선택한 다음 기기의 방향을 변경하면 위의 예와 동일한 문제가 발생할 수 있습니다. 틀린 부분이 무엇인지 확실하지 않아 프레임이 잘못 설정되었습니다. 다른 지점에서 setupScales를 실행하려고 시도했지만 문제가 변경되지는 않습니다.

답변

0

scrollView의 contentSize는 프레임이 변경 될 때 재설정됩니다. 회전이 완료되면 다시 이미지 크기로 설정해야합니다.

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    // When the orientation is changed the contentSize is reset when the frame changes. Setting this back to the relevant image size 
    self.mainScrollView.contentSize = self.mainImageView.image.size; 
    // Reset the scales depending on the change of values 
    [self setupScales]; 
} 

내가 앞으로이 필요 누군가를위한 REPO 설정을 가지고 - https://github.com/StuartMorris0/SPMZoomableUIImageView