Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS controls hide and gone like android

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.

like image 531
Prasanth S Avatar asked Mar 28 '14 06:03

Prasanth S


3 Answers

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]];
like image 71
Nevin Chen Avatar answered Nov 16 '22 05:11

Nevin Chen


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;
like image 36
Rakesh Avatar answered Nov 16 '22 05:11

Rakesh


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

like image 1
The4thIceman Avatar answered Nov 16 '22 07:11

The4thIceman