Horray
Horray

Reputation: 693

How to move a UIView dynamically with auto layout applied?

I have a UISegmentedcontrol (with 2 segments), and a UIView. Due to the fact that the minimum number of segments is 2, I wan't to hide the segmentedcontrol and add a UILabel.

This is what it looks like in the simulator:

enter image description here (The red is the UIView.)

Here is my code:

[self.segment setHidden:YES];

CGRect segmentFrame = self.segment.frame;
segmentFrame.size.width /= 2;

UILabel *myLabel= [[UILabel alloc] initWithFrame:segmentFrame];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:myLabel];

As you can see at the picture above, I have auto-layout applied. When the view get's hidden and the label gets added, the view doesn't get moved over to the left. So I tried moving it programmatically (which probably shouldn't be done when auto-layout is applied):

CGRect myViewFrame = self.myView.frame;
myViewFrame.origin.x -= segmentFrame.size.width;
self.myView.frame = myViewFrame;

That still didn't work. My question is: How can I get the view to move over to the left when the segmentedcontrol becomes hidden?

Upvotes: 4

Views: 1316

Answers (3)

Rich Tolley
Rich Tolley

Reputation: 3842

Hook up the segmented control's width constraint to a property and programmatically change its value to 0, then re layout the view (You may also wish to zero the gap between the segmented control and the view -use the same approach there).

Upvotes: 1

ShahiM
ShahiM

Reputation: 3268

If you are using autolayout, a better approach is to modify the autolayout constraints programmatically.

Follow these steps :

create an IBOutlet in your viewController.h file :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameButtonVerticalConstraint;

now connect it with the concerned autolayout constraint :

enter image description here

Now you can simply change the constraint like this :

self.nameButtonVerticalConstraint.constant=25;

And if you want smoother transitions, you can put it inside an animation block :

[UIView animateWithDuration:1.2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    self.nameButtonVerticalConstraint.constant=50;
    [self.view layoutIfNeeded];
} completion:nil];

EDIT : here's an excerpt from this article

If other constraints will be affected because of the update to the constraint above, the following must also be called:

 [viewForUpdate setNeedsUpdateConstraints];

Now, animate your view by calling layoutIfNeeded inside your animation block.

Upvotes: 2

MSU_Bulldog
MSU_Bulldog

Reputation: 3519

Add a constraint for the leading space to superview. In your .h:

IBOutlet NSLayoutConstraint *leftSpace;

Drag that from your connections tab in Interface Builder to the constraint you have set for the leading space to superview. Wherever you are trying to move it programmatically in your .m file, set the leftSpace property to the width that you would like, like this:

leftSpace.constant = *the width you would like*;

Hope that helps!

Upvotes: 1

Related Questions