由於原來的問題是關係到Xamarin,我提供完整的C#解決方案。
首先,創建高度約束爲您的視圖,並給它在Xcode界面生成器的標識符:
![Constraint Identifier](https://i.stack.imgur.com/gzdGu.png)
然後,在控制器倍率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;
}
}
不幸的是,我使用Xamarin(與VS2012插件),而不必訪問故事板。約束是手動創建的。我無法訪問這些屬性。 –