2016-06-17 5 views
0

Ich versuche, ein UIlabel zu einer UIView-Klasse hinzuzufügen. sollte es im folgenden Format sein -15-Label (Stretch bis zur maximalen Breite) -15.AwakeFromNib Auto-Layout-Stretch UIlabel auf Abstand auf beiden Seiten programmgesteuert

Top Abstand = 15 und Höhe fixiert auf 30

Zwei Probleme mit dem folgenden Code: - 1) Label bis max nicht strecken Breite 2) Rechte Seite Abstand wird nicht angezeigt, wenn der Text es ist zu lang.

-(void)awakeFromNib{ 
    [super awakeFromNib]; 
    view1 =[[UILabel alloc] init]; 

    view1.translatesAutoresizingMaskIntoConstraints=NO; 

    [self addSubview:view1]; 
    view1.text= @"Hello"; 

    NSDictionary *constraintViews= 
    @{@"view1":view1}; 
    NSDictionary *[email protected]{@"spacing":@(15)}; 
    NSArray *hConstraints=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-spacing-[view1]-spacing-|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:allViews]; 


    NSArray *vConstraints=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-spacing-[view1(30)]" options:0 metrics:metrics views:constraintViews]; 
    [self addConstraints:hConstraints]; 
    [self addConstraints:vConstraints]; 
} 

Antwort

0

1)

  • aktualisieren die horizontalen Randbedingungen wie folgt:

    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-spacing-[view1][email protected]|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:constraintViews]; 
    
  • Try Hinzufügen der Zeile unter:

    [view1 setContentHuggingPriority:UILayoutPriorityHigh forAxis:UILayoutConstraintAxisHorizontal]; 
    

2) Ich setze die numberOfLines Eigenschaft eines Labels standardmäßig auf 0, so dass das Label vertikal automatisch autorisiert wird, wenn der Text in zwei oder mehr Zeilen angezeigt werden muss. Davon abgesehen, müssen Sie die feste Höhe Einschränkung und das Etikett wird die Größe des Inhalts den es entfernen, etwa so:

view1.numberOfLines = 0; 
NSArray *vConstraints=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-spacing-[view1]" options:0 metrics:metrics views:constraintViews]; 

Ich hoffe, das hilft.

+0

Horizontale Beschränkung funktioniert immer noch nicht, es zeigt Abstand von 15 auf der Vorderseite, aber auf der Rückseite nicht. – andyPaul

+0

Versuchen Sie, die Priorität dieser Einschränkung auf 1000 (erforderlich) zu erhöhen. [NSLayoutConstraint constrictsWithVisualFormat: @ "H: | -spacing- [view1] -spacing @ 751- |" Optionen: NSLayoutFormatAlignAllCenterX Metriken: Metriken views: constraintViews]; – user4955208

+0

Es hat immer noch nicht funktioniert. Ich weiß nicht, warum die linke Seite richtigen Abstand zeigt, rechte Seite gibt es keinen Abstand – andyPaul

0

Ich habe diese generische Methode zum Anwenden von Constraints von childView wrt auf ParentView verwendet. Übergeben Sie einfach Ihre Ansichten an diese Methode.

+ (void)applyConstraints:(UIView *)pChildView withSuperView:(UIView *)pParentView { 
pChildView.translatesAutoresizingMaskIntoConstraints = NO; 
// Width. 
CGFloat widthValue = pParentView.frame.size.width; 
[pParentView addConstraint:[NSLayoutConstraint constraintWithItem:pChildView attribute:NSLayoutAttributeWidth 
                 relatedBy:NSLayoutRelationEqual toItem:pParentView 
                 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:widthValue]]; 
// Height. 
CGFloat heightValue = pParentView.frame.size.height; 
[pParentView addConstraint:[NSLayoutConstraint constraintWithItem:pChildView attribute:NSLayoutAttributeHeight 
                 relatedBy:NSLayoutRelationEqual toItem:pParentView 
                 attribute:NSLayoutAttributeHeight multiplier:1.0 constant:heightValue]]; 
// X margin. 
[pParentView addConstraint:[NSLayoutConstraint constraintWithItem:pChildView attribute:NSLayoutAttributeCenterXWithinMargins 
                 relatedBy:NSLayoutRelationEqual toItem:pParentView 
                 attribute:NSLayoutAttributeCenterXWithinMargins multiplier:1.0 constant:0]]; 
// Y margin. 
[pParentView addConstraint:[NSLayoutConstraint constraintWithItem:pChildView attribute:NSLayoutAttributeCenterYWithinMargins 
                 relatedBy:NSLayoutRelationEqual toItem:pParentView 
                 attribute:NSLayoutAttributeCenterYWithinMargins multiplier:1.0 constant:0]]; 
} 
Verwandte Themen