ios:我的vfl约束被打破了

时间:2013-08-14 17:12:38

标签: ios constraints visual-format-language

我的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];

}

1 个答案:

答案 0 :(得分:0)

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

[[self useLocationButton] setTranslatesAutoresizingMaskIntoConstraints:NO];