I have used storyboard with autolayout for my UI design. Basically in android there are three different properties will be there like Visible, Invis
You should create IBOutlets for each of the three subviews. Then you can show/hide each of them directly from those references. If you hide a view, it will automatically hide its subviews.
@IBOutlet var yourStackView: UIStackView! yourStackView.hidden = true;
Another solution:
If you have tags for each view you can hide and display them using:
Objective C
For Hiding:
[[self.view viewWithTag:1] setHidden:YES];
Showing:
[[self.view viewWithTag:1] setHidden:NO];
In Swift:
Hiding:
self.view.viewWithTag(1)?.hidden = true
Showing:
self.view.viewWithTag(1)?.hidden = false
Note: It's just for hide view not for space reducing. If you need to reduce space with hide view, you need to constraint increase decrease with hide/show programmatically. And stack view do it automatically where you just change the actual height of stack view with hide/show.
May be it will help you otherwise you can ask me.
To remove the space occupied by a view(control) can either reduce the size
of its frame to zero or remove it from the view hierarchy. I.e. by calling removeFromSuperview
on the control.
For example if you have to remove the space occupied by a UITextField
(say CONTROLNAME
), then you can either use:
CGRect tempFrame = CONTROLNAME.frame;
CGSize currentSize = tempFrame.size; //for later use
tempFrame.size = CGSizeZero;
CONTROLNAME.frame = tempFrame;
or
CGRect currentFrame = CONTROLNAME.frame; //for later use
[CONTROLNAME removeFromSuperview];
UPDATE:
In the first case you will have to store the earlier size to bring back the control to its initial position.
CGRect tempFrame = CONTROLNAME.frame;
tempFrame.size = currentSize; //set to initial value
CONTROLNAME.frame = tempFrame;
In the second case you will have to store the frame of the control to bring it back to its initial position (and also the control itself if it is a local variable or weak instance variable).
CONTROLNAME.frame = currentFrame;
Neither removing the subview, nor adjusting the frame worked for me, so as an alternate solution, I programmatically added a constraint that automatically adjusts the difference.
For example: If you have 3 views, A_view B_view and C_view vertically aligned in that order and you want to "Hide" B and also adjust the difference, add a constraint
B_view.removeFromSuperView()
var constr = NSLayoutConstraint(item: C_view,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: A_view,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1,
constant: 20)
view.addConstraint(constr)
constant is (in this case) the amount of vertical space between C_view and A_view
It worked for me, but requires knowledge of constraints
if your view for example
@property (weak, nonatomic) IBOutlet SearchBarView *searchBar;
already has a constraint with it. Add a new IBLayout by dragging your constraint to the .h file.ex:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintSearBarHeight;
and do this in where ever you like
self.constraintSearBarHeight.constant = 0;
if your view don't have a constraint yet. I found this answer helpful. Just do below
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]];