【问题标题】:How to hide a UIView and remove the "empty" space? -iOS/Monotouch如何隐藏 UIView 并删除“空白”空间? -iOS/单触
【发布时间】:2013-06-24 21:29:30
【问题描述】:

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

有谁知道实现此目的的干净方法?

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


例子:

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

应该是这样渲染的:

UIButton | UIButton | UIButton
UIButton 

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

【问题讨论】:

    标签: ios xamarin.ios constraints xamarin autolayout


    【解决方案1】:

    由于原始问题与 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;
        }
    }
    

    【讨论】:

    • 感谢您的代码,但遗憾的是,当我使用此代码时,我仍然有空白空间:(
    【解决方案2】:

    在情节提要中首先连接您的约束。那就试试这个

        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];
        }];
    

    【讨论】:

    • 不幸的是,我使用的是 Xamarin(和 VS2012 插件)并且无法访问情节提要。约束是手动创建的。我无权访问这些属性。
    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    相关资源
    最近更新 更多