0

I know about setting constraint in InterfaceBuilder ex. Leading, trailing, top, bottom, fixed width etc.. I found some constraint code, I don't know what this code trying to set which constraint, What is exactly meaning of below visual format constraints?

  NSDictionary *binding = @{@"v" : self.view};
    NSDictionary *metrics = @{@"height" : @(self.height)};
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:binding]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[v(==height)]|" options:0 metrics:metrics views:binding]];
Akshay
  • 153
  • 3
  • 13
  • 2
    look here https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html – Reinier Melian Nov 20 '17 at 11:20
  • on that doc `cmd +f` "v". no results. can you tell me What is "v" & "height" ? – Akshay Nov 20 '17 at 11:23
  • v is your view binded in the dictionary while V uppercased is vertical, as well as H is horizontal – Reinier Melian Nov 20 '17 at 11:25
  • 2
    Following website will help you. https://autolayoutconstraints.com – Rahul_Chandnani Nov 20 '17 at 11:26
  • 2
    Dont stick with VFL anymore VFL cant account for safe area layout guides it can only account top layout guides. You can write perfect VFL to screw up your UI in iPhoneX – Sandeep Bhandari Nov 20 '17 at 11:29
  • 1
    There are a lot of compelling arguments to avoid using VFL at this stage of iOS development. Won't work with safe area layout guides as @SandeepBhandari mentions. Also extremely prone to developer error. If you're looking to understand existing constraints, I'd look to translate them to anchor-based. If you want to make new constraints, I'd suggest anchor-based instead. – Connor Neville Nov 20 '17 at 17:38
  • @SandeepBhandari can you look at [this](https://stackoverflow.com/questions/47076230/play-video-in-active-uitableviews-cells) ? – Akshay Dec 28 '17 at 05:21

2 Answers2

4

H:|[v]|

H represents that the constraints are meant to be added horizontally, similarly V is for vertical.

| represents super view as indicated by the binding dictionary. NSDictionary *binding

[v] represents the view itself.

So H:|[v]| resolves to leading & trailing constraints with 0 constant.

V:[v(==height)]|

Similarly here, view is given a bottom constraint and a height constraint with a constant height as mentioned in NSDictionary *metrics.

Please refer https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html for more information.

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
0

As GoodSp33d suggested to me.

Your constraint is-

(1) leading & trailing to self.view is 0 I have converted above constraint into diffrent way

(2) ContentView's bottom assigned to self.view

(3)Constant height constraint

Constraint in another form as-

    [self.contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
    [self.contentView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
    [self.contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;


    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:self.height];
    [self.view addConstraint:heightConstraint];
Akshay
  • 153
  • 3
  • 13