2015-01-01 43 views
0

我可以配置我的UIViewControlleredgesForExtendedLayout,這樣它將在導航欄或標籤欄等內容之下進行擴展。如果我這樣做,有沒有辦法來確定框架是不是被遮蔽查找UIViewController的視圖的邊界不被UINavigationBar,UITabBar(等)遮擋

作爲一種可能的替代方案,有沒有辦法讓UIViewController確定默認的contentInset應用於UIScrollView它包含的內容?

使用案例

我有zoomable UIScrollView包含圖像。

當它完全縮小時,我想調整內容插入,也讓內容保持居中(details here)。然而,我修改後的插圖並不考慮自動應用的插入內容,以便其內容不會被導航欄遮擋等。

我還需要計算內容的最小縮放比例 - 即整個圖像將可見並且不被遮擋。爲了計算這一點,我需要知道內容視圖中未被遮擋部分的大小。

回答

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:…方法是在觀看動畫塊中調用,所以它適用當你從橫向切換到肖像將導致很好的流暢的動畫變化的變化。