Quickly how to hide an element and its space

Hope the title was clear. I want to hide an element (data collector in my case), and also I want to hide my space. So I tried this with animation:

@IBAction func showQnt(sender: AnyObject) { if (self.pickerQnt.alpha == 0.0){ UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: { self.pickerQnt.alpha = 1.0 }, completion: { (finished: Bool) -> Void in //var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 162) //self.pickerQnt.addConstraint(constH) }) } else { UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: { self.pickerQnt.alpha = 0.0 }, completion: { (finished: Bool) -> Void in // CHECK: ?!? constrain to set view height to 0 run time //var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0) //self.pickerQnt.addConstraint(constH) }) } } 

I also tried something like:

 self.pickerQnt.hidden = true 

but does not work.

Thanks in advance.

+7
source share
2 answers

Using the โ€œhiddenโ€ property on views in Objective-C / Swift does not actually โ€œcrashโ€ the space in the view (unlike โ€œView.GONEโ€ in Android ).

Instead, you should use Autolayout and a height limit.

Create a height limit in your Xib / Storyboard file. Give him the desired height. Make an IBOutlet for this restriction (ctrl-drag from the list of restrictions in the Constraints list to the source file), and then you can write this method

Fast decision

 var pickerHeightVisible: CGFloat! ... ... func togglePickerViewVisibility(animated: Bool = true) { if pickerHeightConstraint.constant != 0 { pickerHeightVisible = pickerHeightConstraint.constant pickerHeightConstraint.constant = 0 } else { pickerHeightConstraint.constant = pickerHeightVisible } if animated { UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }, completion: nil) } else { view.layoutIfNeeded() } } 

Objective-C solution:

 @property (nonatomic, strong) CGFloat pickerHeightVisible; ... ... - (void)togglePickerViewVisibility:(BOOL)animated { if(pickerHeightConstraint.constant != 0) { pickerHeightVisible = pickerHeightConstraint.constant; pickerHeightConstraint.constant = 0; } else { pickerHeightConstraint.constant = pickerHeightVisible; } if(animated) { [UIView animateWithDuration:0.2 animations:(void (^)(void))animations:^(void) { [self.view layoutIfNeeded]; }]; } else { [self.view layoutIfNeeded]; } } 

DISCLAIMER . I have not tested or compiled the code above, but it will give you an idea of โ€‹โ€‹how to achieve it.

IMPORTANT The code above assumes that you specify a height limit in the selection view that is greater than 0 in the / nib storyboard.

+7
source

This is an old question, but there is another option that has become available for newer versions of iOS:

If your layout allows this, and if you focus on iOS9 or later, you can arrange your presentation in UIStackView as a container. The child representations of the stack are destroyed when hidden, i.e. no longer take up space.

Dynamically change the contents of stack views

The stack view automatically updates its layout whenever views are added, deleted, or inserted into the layoutSubviews array, or when one of the isHidden properties of the ordered subview changes.

( https://developer.apple.com/documentation/uikit/uistackview#overview )

0
source

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


All Articles