Show / hide prefs iphone interface elements - how?

I have a simple form in an application for iPhone. The form is laid out and managed through IB and typical posting (i.e. I do not create this form programmatically).

One of the fields (and the label associated with it) should be displayed only if a specific preference is specified.

I could set the field and label alpha to 0 and disable them in this case. The problem is that the fields below this now invisible field will remain in the same place and there will be a large empty area. My goal is to make the screen look normal in any state.

Is there a way to programmatically remove (or add) user interface elements and change them up or down to make room? Or should I consider creating a whole other NIB file for this second case? (and if I do, is there an easy way to share common elements?)

Current user interface with two controls shown

With both http://img.skitch.com/20100704-bm41w6wtqkdgh1da99ihb7g32d.jpg

User interface with additional control hidden through alpha == 0

Using Alpha to hide http://img.skitch.com/20100704-q2sxrj3nf6ya68wp6ubn86n2pa.jpg

Desired user interface with additional hidden control

Desirable when hidden http://img.skitch.com/20100704-82r876pgctee8gb51ujg1dwj7k.jpg

+6
iphone cocoa-touch interface-builder
source share
3 answers

When each user interface element is associated with an IBOutlet pointer, for example.

@property (nonatomic, retain) IBOutlet UITextField *field_a; @property (nonatomic, retain) IBOutlet UITextField *field_b; @property (nonatomic, retain) IBOutlet UITextField *field_c; // ... 

You can check the visibility of each element with:

 if (field_a.hidden) { // ... } else { // ... } 

And move them:

 CGPoint pt = field_a.center; pt.y -= 60; field_a.center = pt; 

Or some animation:

 CGPoint position = field_a.center; position.y -= 60; [UIView beginAnimations:@"MoveUp" context:NULL]; [UIView setAnimationDuration:0.5]; field_a.center = position; [UIView commitAnimations]; 

To hide an item:

 field_a.hidden = YES; 

To show an item:

 field_a.hidden = NO; 
+7
source share

Use the cocoa touch properties:

.hidden 1 .userInteractionEnabled 0

Or you could:

.alpha = 0

+1
source share

Recently, I saw a tutorial about this that included moving the sub-view down in the main view when a segmented control was selected. I believe this was an animation called beginAnimations: context:, but I cannot find a link to this tutorial right now.

Essentially, there were views under the guise that were hidden, and one set was removed from the path, and other controls were hidden.

0
source share

All Articles