My VFL constraint keeps breaking, and I can't figure out why. It's a UIButton with a custom background. I want it to have a flexible width, while maintaining a space of 10 pixels from either side of the view. Here's the code:

- (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];

}
有帮助吗?

解决方案

The answer was that I set the 'setTranslatesAutoresizingMaskIntoConstraints' on the parent view and not the button. That line should read:

[[self useLocationButton] setTranslatesAutoresizingMaskIntoConstraints:NO];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top