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