'NSInvalidArgumentException', reason: 'Unable to parse constraint format'

前端 未结 4 1280
梦如初夏
梦如初夏 2021-01-30 16:18

I have a subview that I want to keep stops during rotating screen, so I decided to put the NSLayoutConstraint type:

Trailing Space to Superview
Top Space to Supe

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 16:40

    It looks like that the autolayout visual format parsing engine is interpreting the "." in your VFL constraint to be a keyPath instead of a key like it's using valueForKeyPath:.

    NSDictionaryOfVariableBindings(...) will take whatever your parameter is in the parenthesis and translate it into a literal key with the object as the value (in your case: @{"self.arrow" : self.arrow}). In the case of the VFL, autolayout is thinking that you have a key named self in your view dictionary with a subdictionary (or subobject) that has a key of arrow,

    @{
       @"self" : @{ @"arrow" : self.arrow }
    }
    

    when you literally wanted the system to interpret your key as "self.arrow".

    Usually, when I'm using a instance variables getter like this, I typically end up creating my own dictionary instead of using NSDictionaryOfVariableBindings(...) like so:

    NSDictionary *views = @{ @"arrowView" : self.arrow }
    

    or

    NSDictionary *views = NSDictionaryOfVariableBindings(_arrow);
    

    Which would allow you to use the view in your VFL without the self and you still know what you're talking about:

    NSArray *arrowHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[arrowView]-5-|" options:0 metrics:nil views];
    

    or

    NSArray *arrowHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_arrow]-5-|" options:0 metrics:nil views];
    

    As a general rule, I've learned not to have dictionary keys with a dot (.) in them to avoid any system confusion or debugging nightmares.

提交回复
热议问题