How to hide UIView and remove the "empty" space? -iOS / MonoTouch

I need to be able to hide the controls on a page that uses restrictions and removes the white space that Hidden=true leaves. It should be similar to how the web handles visibility. If he is invisible, he does not take up space.

Does anyone know how to do this?

Please let me know if you need more information. thanks


Example:

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

This really needs to be done as follows:

 UIButton | UIButton | UIButton UIButton 

Edit: I am using Xamarin Studio and VS2012 for development.

+4
source share
2 answers

In the storyboard, first lay down your limitations. Then try this

  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]; }]; 
+1
source

Since the original question is related to Xamarin, I provide a complete C # solution.

First create a height limit for your view and give it an identifier in Xcode Interface Builder:

Restriction id

Then, in the controller, override the ViewDidAppear () method and view using the HidingViewHolder:

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

It is important to create a HidingViewHolder when the view has been drawn, so it has a real height. To hide or show the view, you can use the appropriate methods:

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

HidingViewHolder Source:

 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; } } 
0
source

Source: https://habr.com/ru/post/1487961/


All Articles