2013-06-24 95 views
4

我需要能夠在使用約束的頁面上隱藏控件,並刪除Hidden=true離開的空白區域。它需要與網絡處理可見性的方式類似。如果它不可見,則不佔用空間。如何隱藏UIView並刪除「空白」空間? -iOS/Monotouch

有誰知道一個乾淨的方式來完成這個?

如果您需要更多詳細信息,請讓我知道。 THX


例子:

UIButton | UIButton | UIButton 
"empty space for hidden UIButton" 
UIButton 

這確實應該呈現:

UIButton | UIButton | UIButton 
UIButton 

編輯:我使用Xamarin Studio和VS2012發展。

回答

1

在故事板中首先綁定約束。那就試試這個

self.viewToHideHeight.constant = 0; 
    self.lowerButtonHeightFromTop.constant = self.viewToHideHeightFromTop.constant + self.viewToHideHeight.constant; 

    [UIView animateWithDuration:0.5f animations:^{ 
     self.viewToHide.alpha = 0.0f; 
     [self.view layoutIfNeeded]; 
    }]; 
+0

不幸的是,我使用Xamarin(與VS2012插件),而不必訪問故事板。約束是手動創建的。我無法訪問這些屬性。 –

0

由於原來的問題是關係到Xamarin,我提供完整的C#解決方案。

首先,創建高度約束爲您的視圖,並給它在Xcode界面生成器的標識符:

Constraint Identifier

然後,在控制器倍率ViewDidAppear()方法,並用HidingViewHolder包裹視圖:

public override void ViewDidAppear(bool animated) 
    { 
     base.ViewDidAppear(animated); 
     applePaymentViewHolder = new HidingViewHolder(ApplePaymentFormView, "ApplePaymentFormViewHeightConstraint"); 
    } 

視圖佈局時創建HidingViewHolder非常重要,因此它具有分配的實際高度。 隱藏或顯示視圖,您可以使用相應的方法:

applePaymentViewHolder.HideView(); 
applePaymentViewHolder.ShowView(); 

HidingViewHolder來源:

using System; 
using System.Linq; 
using UIKit; 

/// <summary> 
/// Helps to hide UIView and remove blank space occupied by invisible view 
/// </summary> 
public class HidingViewHolder 
{ 
    private readonly UIView view; 
    private readonly NSLayoutConstraint heightConstraint; 
    private nfloat viewHeight; 

    public HidingViewHolder(UIView view, string heightConstraintId) 
    { 
     this.view = view; 
     this.heightConstraint = view 
      .GetConstraintsAffectingLayout(UILayoutConstraintAxis.Vertical) 
      .SingleOrDefault(x => heightConstraintId == x.GetIdentifier()); 
     this.viewHeight = heightConstraint != null ? heightConstraint.Constant : 0; 
    } 

    public void ShowView() 
    { 
     if (!view.Hidden) 
     { 
      return; 
     } 
     if (heightConstraint != null) 
     { 
      heightConstraint.Active = true; 
      heightConstraint.Constant = viewHeight; 
     } 
     view.Hidden = false; 
    } 

    public void HideView() 
    { 
     if (view.Hidden) 
     { 
      return; 
     } 
     if (heightConstraint != null) 
     { 
      viewHeight = heightConstraint.Constant; 
      heightConstraint.Active = true; 
      heightConstraint.Constant = 0; 
     } 
     view.Hidden = true; 
    } 
}