2013-06-24 93 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; 
    } 
}