0

Here I have designs for a custom arrow which will function as a share button. It is supposed to be pinned to the lower edge, with its center point being anchored to the bottom view as pictured.

enter image description here

How would I do this using VFL?

This is what I attempted

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_whiteSquare(160)]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_shareButton(45)]-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_shareButton(45)]-20-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];



[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_whiteSquare]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];

Where both whiteSquare and shareButton are appended to the superview. But this just puts shareButton at the bottom of the superview, not center anchored to the bottom of whiteSquare

CQM
  • 42,592
  • 75
  • 224
  • 366

1 Answers1

1

You should position the share button relative to the white square and not the super view

This should do the trick:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_whiteSquare(160)]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_whiteSquare]-22.5-[_shareButton(45)]"
                                                                  options: NSLayoutFormatDirectionRightToLeft
                                                                  metrics:nil
                                                                    views:elementDict]];


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_whiteSquare]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:elementDict]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_whiteSquare]-22.5-[_shareButton(45)]"
                                                                  options:NSLayoutFormatDirectionRightToLeft
                                                                  metrics:nil
                                                                    views:elementDict]];
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • 1
    this put me on the right track but I had to change options to `NSLayoutFormatDirectionLeadingToTrailing` instead of Right to Left, and instead of 22.5 I had to use negative values, which I hadn't previously considered – CQM Jul 29 '15 at 14:53