2015-01-01 5 views
0

UIViewControlleredgesForExtendedLayout을 탐색 모음이나 탭 표시 줄과 같은 내용 아래로 확장하도록 구성 할 수 있습니다. 내가 이렇게하면 프레임을 결정할 수있는 방법이 있습니까 이 아닌 가려져?UIViewController의 경계가 UINavigationBar, UITabBar 등으로 가려지지 않는 부분을 찾습니다.

가능한 대안으로 UIViewController에 포함 된 UIScrollView에 적용 할 기본 contentInset을 결정하는 방법이 있습니까?

사용 사례

확대/축소 가능한 이미지가 포함 된 UIScrollView가 있습니다.

완전히 축소 한 경우 콘텐츠를 중앙에 놓아 두도록 콘텐츠 삽입을 조정하려고합니다 (details here). 그러나 내 수정 된 인셋은 UIViewController이 자동으로 적용되는 인셋을 고려하지 않으므로 해당 내용이 탐색 모음 등으로 가려지지 않습니다.

또한 콘텐츠의 최소 확대/축소를 계산해야합니다. 전체 이미지가 보이고 어둡지 않게됩니다. 이것을 계산하기 위해, 나는 내용보기의 방해받지 않는 부분의 크기를 알아야한다.

답변

0

당신이

-(CGRect) unobscuredBounds 
{ 
    CGRect bounds = [self.view bounds]; 
    return UIEdgeInsetsInsetRect(bounds, [self defaultContentInsets]); 
} 

-(UIEdgeInsets) defaultContentInsets 
{ 
    const CGFloat topOverlay = self.topLayoutGuide.length; 
    const CGFloat bottomOverlay = self.bottomLayoutGuide.length; 
    return UIEdgeInsetsMake(topOverlay, 0, bottomOverlay, 0); 
} 

당신은 쉽게 재사용에 대한 category에 넣고 수 있어야합니다.

이 메서드는 회전 후에 뷰 크기가 조정될 때 발생하는 변경 사항을 올바르게 처리합니다. 즉, UINavigationBar 크기의 변경이 올바르게 처리됩니다. centre content by adjusting insets이를 사용하려면 내용에게

을 중심으로

, 당신은 같은 것을 할 것 :

-(void) scrollViewDidZoom:(UIScrollView *)scrollView 
{ 
    [self centerContent]; 
} 

- (void)centerContent 
{ 
    const CGSize contentSize = self.scrollView.contentSize; 
    const CGSize unobscuredBounds = [self unobscuredBounds].size; 

    const CGFloat left = MAX(0, (unobscuredBounds.width - contentSize.width)) * 0.5f; 
    const CGFloat top = MAX(0, (unobscuredBounds.height - contentSize.height)) * 0.5f; 

    self.scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left); 
} 

귀하의 콘텐츠 세트 지금 (은폐되지 않도록하기 위해)이 필요로하는 기본 세트의 값을 반영합니다 또한 중심에 잘 맞게 삽입해야 할 필요가 있습니다. 회전 & 확대

처리

당신은 아마도 가로 및 세로 사이에 애니메이션을 때를 중심으로 수행 할. 동시에 콘텐츠가 항상 맞을 수 있도록 최소 확대/축소 배율을 조정할 수 있습니다.

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self centerContent]; 

    const bool zoomIsAtMinimum = self.scrollView.zoomScale == self.scrollView.minimumZoomScale; 

    self.scrollView.minimumZoomScale = [self currentMinimumScale]; 

    if(zoomIsAtMinimum) 
    { 
     self.scrollView.zoomScale = self.scrollView.minimumZoomScale; 
    } 
} 

-(CGFloat) currentMinimumScale 
{ 
    const CGFloat currentScale = self.scrollView.zoomScale; 
    const CGSize scaledContentSize = self.scrollView.contentSize; 
    const CGSize scrollViewSize = [self unobscuredBounds].size; 

    CGFloat scaleToFitWidth = currentScale * scrollViewSize.width/scaledContentSize.width; 
    CGFloat scaleToFitHeight = currentScale * scrollViewSize.height/scaledContentSize.height; 

    return MIN(scaleToFitWidth, scaleToFitHeight); 
} 

willAnimateRotationToInterfaceOrientation:… 방법은보기 애니메이션 블록 내에서 호출, 그래서 당신이 가로에서 세로 전환으로 좋은 부드러운 애니메이션의 변화로 이어질 것입니다 적용되는 변경 :이 같은 것을 사용해보십시오.