Pergunta

I have some expandable views within a UIScrollView and when those expand, I need all views beneath them to automatically move down however much the expanded view expanded.

What I want to happen: (left being starting position, right being after frame change)

what i want

What actually is happening: what I get

Several other views need to move as well that would have a fixed space to the yellow view bottom. I assume (hope) if I can get the yellow view to move down when blue view expands, those will move as well.

I am updating the size by blueView.frame = newFrame; where new frame just has a larger height.

Is there a simple way to do this with constraints, which will not require a significant amount of code? I haven't quite been able to figure it out.

Foi útil?

Solução 2

I merely did not understand the amount of work that was required for constraints (not that it's too excessive).
I thought I would just have to set vertical spacing and then frame changes to blue view would move yellow view. I had to configure a few other constraints as well (not using the 'Add Missing Constraints' option) to remove all the warnings and now it works as expected.

Outras dicas

Below is the code that i used recently for similar purpose,

- (void)viewDidLoad
{
[super viewDidLoad]; 
_expandableView = [[UIView alloc] init];
_adjacentView = [[UIView alloc] init];

[_expandableView setBackgroundColor:[UIColor redColor]];
[_scrollView addSubview:_expandableView];
_expandableView.translatesAutoresizingMaskIntoConstraints = NO;

[_adjacentView setBackgroundColor:[UIColor blueColor]];
[_scrollView addSubview:_adjacentView];
_adjacentView.translatesAutoresizingMaskIntoConstraints = NO;

NSArray *constraints = nil;
NSDictionary *views = NSDictionaryOfVariableBindings(_expandableView,_adjacentView);

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_expandableView]-10-[_adjacentView(==20)]-|" options:0 metrics:nil views:views];
[_scrollView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_expandableView(==20)]" options:0 metrics:nil views:views];
heightConstraint = [constraints lastObject];
[_scrollView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_expandableView(==800)]-|" options:0 metrics:nil views:views];
[_scrollView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_adjacentView(==800)]-|" options:0 metrics:nil views:views];
[_scrollView addConstraints:constraints];

}

- (IBAction)expandAction:(id)sender
{
  heightConstraint.constant = heightConstraint.constant+20;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top