How to move UIView dynamically using auto layout?

I have UISegmentedcontrol(with two segments) and a UIView. Due to the fact that the minimum number of segments is 2, I will not hide segmentedcontroland add UILabel.

Here's what it looks like in a simulator:

enter image description here (Red - 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 in the picture above, I use auto-layout. When it is viewhidden and added label, it viewdoes not move to the left. Therefore, I tried to move it programmatically (which probably should not be done when applying automatic layout):

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

It still didn't work. My question is: how can I make it viewmove to the left when it is hiding segmentedcontrol?

+4
3

autolayout, autolayout constraints.

:

IBOutlet viewController.h:

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

:

enter image description here

:

self.nameButtonVerticalConstraint.constant=25;

, :

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

EDIT: this article

- , :

 [viewForUpdate setNeedsUpdateConstraints];

, layoutIfNeeded .

+2

. .h:

IBOutlet NSLayoutConstraint *leftSpace;

Interface Builder , . .m , leftSpace , :

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

, !

+1

0, (, - ).

+1

Source: https://habr.com/ru/post/1583855/


All Articles