Swift how to hide element and his space

Related searches

i hope that title was clear. I want to hide an element (data picker in my case) and also i want to hide his space. So i tried this with an 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've also tried something like:

self.pickerQnt.hidden = true

but wont work.

Thanks in advance.

Using the "hidden" property on views in Objective-C/Swift does as a matter of fact not "collapse" the space the view is taking (as opposed to "View.GONE" in Android).

Instead you should use Autolayout and a Height constraint.

Create a Height constraint in your Xib/Storyboard file. Giving it the wished height. Make an IBOutlet to that constraint (ctrl-drag from the Constraint in the Constraints list to your source file), and then you can write this method

Swift solution

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 nor compiled the code above, but it will give you an idea of how to achieve it.

IMPORTANT: The code above assumes that you give the height constraint of the picker view a value greater than 0 in the storyboard/nib.

isHidden, isHidden. A Boolean value that determines whether the view is hidden. Availability. iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+. Framework. UIKit. On This Page. A scale (0) or translate (-999px, 0px) off-screen will hide the element: transform offers excellent performance and hardware acceleration because the element is effectively moved into a separate

This is an old question but there is another option that became available for newer iOS versions:

If your layout allows it, and if you are targeting iOS9 or later you can arrange your view in a UIStackView as the container. Children of stack views collapse when hidden, i.e. no longer take any space.

Dynamically Changing the Stack View’s Content

The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s isHidden property changes.

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

Animating UIStackView subview layout, By setting the button to be hidden and have alpha 0, we're saying "don't show it to the user, and don't let it take up any space in the stack view." We want to show� A visual element that can be used to separate other content. iOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+ Framework. Swift UI; On This Page

Best way is to add views in stack and upon hiding one view height of the stack view will be automatically adjusted. But if views are not in stack view then just do the following.

1- Hide the view. 2- Assign height constraint to view, make height constraint outlet of the view

@IBOutlet weak var myView: UIView!
@IBOutlet weak var myViewHeightConstraint: NSLayoutConstraint!
myView.isHidden = true
viewHeightConstraint.constant = 0

hide/show text fields with auto layouts and programmatically , how to hide text field that fills the existing text field in iOS, with auto layout.Duration: 10:53 Posted: Nov 16, 2016 Beaver Builder is amazing and your life is going to be so much easier than the other two methods described in this post to show and hide different content! Below is the easiest way to show and hide different content on mobile devices all thanks to the built in features of Beaver Builder .

One simple method is to set the height to zero when hiding the view (that too without making IBOutlet of height constraint).

myView.isHidden = true
myView.heightAnchor.constraint(equalToConstant: CGFloat(0)).isActive = true

And set height again when unhiding

myView.isHidden = false
myView.heightAnchor.constraint(equalToConstant: CGFloat(40)).isActive = true

To achieve the above functionality with animation, simply put heightAnchor code in UIView.animate() block

If you have to do this many times, then you can reduce your two-line code into one line by making a function in extension.

Hiding UIView's in iOS, This post is about programmatically hiding UIView's in iOS. UIView's are generic containers of other UI interface elements (“widget” doesn't seem to be the right word). Consider an But they will still take up space. This makes� display: none; is commonly used with JavaScript to hide and show elements without deleting and recreating them. Take a look at our last example on this page if you want to know how this can be achieved. The <script> element uses display: none; as default.

Stack View Constraint Conflicts When Hiding Views, You can find the example layout from this post in the RefreshScroll project in my GitHub repository. Never Miss A Post. Sign up to get my iOS� I was wondering, what would the code look like if you wanted to hide or show elements for certain users, in asp.net? Re: Hide asp.net elements thatraja 11-Sep-14 2:17

- Check if array contains element - Check if array is empty - Check if object is an array - Concatenate arrays - Create an empty array - Filter an array based on condition - Find element index in array by value - Get array element at index - Get first element of array - Get last element of array - Get maximum element from array

The issue for me is that if the target element has a script attached and I want the element to start hidden (disabled), it also disables the attached script which I don't want to happen. In DFGUI this was handled by giving UI elements a 'Visible' toggle so you could change the visibility rather than the active state.