2013-08-14 2 views
0

Ma contrainte VFL ne cesse de se rompre, et je n'arrive pas à comprendre pourquoi. C'est un UIButton avec un arrière-plan personnalisé. Je veux qu'il ait une largeur flexible, tout en conservant un espace de 10 pixels de chaque côté de la vue. Voici le code:ios: ma contrainte vfl est rompue

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

} 

Répondre

0

La réponse est que je mets les « setTranslatesAutoresizingMaskIntoConstraints » sur la vue parent et non sur le bouton. Cette ligne devrait se lire comme suit:

[[self useLocationButton] setTranslatesAutoresizingMaskIntoConstraints:NO]; 
Questions connexes