6

我需要能够隐藏使用约束的页面上的控件并删除Hidden=true留下的空白空间。它需要类似于网络处理可见性的方式。如果它是不可见的,它不会占用空间。

有谁知道一个干净的方法来完成这个?

如果您需要更多详细信息,请告诉我。谢谢


例子:

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

那真的应该像这样呈现:

UIButton | UIButton | UIButton
UIButton 

编辑:我正在使用 Xamarin Studio 和 VS2012 进行开发。

4

2 回答 2

3

由于原始问题与 Xamarin 有关,因此我提供了完整的 C# 解决方案。

首先,为您的视图创建高度约束并在 Xcode Interface Builder 中为其指定一个标识符:

约束标识符

然后在控制器中重写 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;
    }
}
于 2017-06-27T07:43:53.337 回答
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];
    }];
于 2013-06-24T22:03:44.063 回答