iOS controls hide and gone like android
Asked Answered
H

4

14

I have used storyboard with autolayout for my UI design. Basically in android there are three different properties will be there like Visible, Invisible and Gone.

For Example:

   1) android:visibility="gone" // used to hide the control and as well as space
      (or)
      CONTROLNAME.setVisibility(View.GONE);
   2)  android:visibility="invisible" // used to hide the control but it will take space
      (or)
      CONTROLNAME.setVisibility(View.INVISIBLE);

In iOS,

objective-c

  1) ?
  2) [CONTROLNAME setHidden:TRUE]; // used to hide the control but it will take space

swift

  1) ?
  2) CONTROLNAME.isHidden = true  // used to hide the control but it will take space

for act as a Gone in iOS i have searched from google but i can't able to find the Solution.

Hecate answered 28/3, 2014 at 6:1 Comment(5)
Look into Autolayout. Check out: How to use auto-layout to move other views when a view is hidden?Assiduous
I think there is no such function but you can do it with autolayot with relative viewBellboy
Although auto-layout will help you laying out other views when the view(or control) is removed, auto-layout in itself won't help you in removing the 'space' occupied by the view.Newsworthy
Look at @nevin chen's solution, i think that one is perfect solution for your caseImmoral
The only way to achieve this is to use 'stacked views' in the Interface builderVendace
N
5

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;
Newsworthy answered 28/3, 2014 at 6:25 Comment(1)
Reply with what code you have written and what o/p you are getting. Otherwise it will be difficult for anyone to help you.Newsworthy
Q
3

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]];
Quincy answered 28/7, 2016 at 0:46 Comment(1)
I should add to this, Interface builder users are not out in the cold on this. You can right drag from a constraint to the source code just as you can from any control and it'll link your constraint to the relevant property in the view controller Very useful thing to know, because programatically trying to find a specific constraint in a Xib is bit of a nightmareBuddleia
M
1

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

Moncton answered 25/2, 2015 at 4:23 Comment(1)
Interface builder users are not out in the cold on this. You can right drag from a constraint to the source code just as you can from any control and it'll link your constraint to the relevant property in the view controller Very useful thing to know, because programatically trying to find a specific constraint in a Xib is bit of a nightmareBuddleia
S
0

iOS does not contain a match for gone that is why you can use another tecknicks. The most common is to use constraint 0 for autolayout

Shooter answered 30/9, 2022 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.