ios:我的vfl约束被破坏了

ios:我的vfl约束被破坏了,ios,constraints,visual-format-language,Ios,Constraints,Visual Format Language,我的VFL限制不断被打破,我不知道为什么。这是一个自定义背景的UIButton。我希望它有一个灵活的宽度,同时保持一个10像素的空间从任何一边的看法。代码如下: - (void)viewDidLoad { UIImage *buttonImage = [[UIImage imageNamed:@"bluebutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)]; UIImage *buttonImag

我的VFL限制不断被打破,我不知道为什么。这是一个自定义背景的UIButton。我希望它有一个灵活的宽度,同时保持一个10像素的空间从任何一边的看法。代码如下:

- (void)viewDidLoad
{  
UIImage *buttonImage = [[UIImage imageNamed:@"bluebutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:@"bluebuttonHighlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[[self view] addSubview:[self useLocationButton]];
[[self view] setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *nameMap = @{@"locationbtn":[self useLocationButton],@"mainview":[self view]};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[locationbtn(>=36)]-10-|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:horizontalConstraints];
[[self useLocationButton] setBackgroundImage:buttonImage forState:UIControlStateNormal];
[[self useLocationButton] setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[[self useLocationButton] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[[self useLocationButton] setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

}

答案是我在父视图而不是按钮上设置了“setTranslatesAutoresizingMaskIntoConstraints”。该行应改为:

[[self useLocationButton] setTranslatesAutoresizingMaskIntoConstraints:NO];