2015-07-10 12 views
25

ich Auto-Layout für meine ViewControllers verwendet haben. Ich habe die V- und H-Positionen in Constraints gesetzt. aber ich möchte wissen, wie man meine Knopfgröße erhöht, wenn es zu 5s, 6 und 6 plus wechselt. Auf diese Weise habe ich Einschränkungen für die Login-Schaltfläche hinzugefügt.Wie Seitenverhältnis Einschränkung programmatisch in ios setzen

NSArray *btncon_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[btnLogin(40)]" options:0 metrics:nil views:viewsDictionary]; 
[btnLogin addConstraints:btncon_V]; 

NSArray *btncon_POS_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[btnLogin]-100-|" options:0 metrics:nil views:viewsDictionary]; 
[self.view addConstraints:btncon_POS_H]; 


NSArray *btncon_POS_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[Title]-130-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-10-[btnLogin]" options:0 metrics:nil views:viewsDictionary]; 

[self.view addConstraints:btncon_POS_V]; 

Aber mein Problem ist, wenn es die Lücke auf der linken und rechten Seite verwalten. Es wird in Iphone 6 und 6 und seit der Höhe behoben. Wie kann ich die Größe entsprechend der Bildschirmgröße erhöhen. Ich denke, dies könnte die AspectRatio sein, aber wie Sie die Seitenverhältnis-Einschränkung in Code festlegen.

+2

prüfen diese Antwort: http://stackoverflow.com/a/12526630/3202193 –

Antwort

51

Like this. Versuch es einmal.

[self.yourview setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.yourview addConstraint:[NSLayoutConstraint 
            constraintWithItem:self.yourview 
            attribute:NSLayoutAttributeHeight 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.yourview 
            attribute:NSLayoutAttributeWidth 
            multiplier:(self.yourview.frame.size.height/self.yourview.frame.size.width) 
            constant:0]]; 

oder anstelle von (self.yourview.frame.size.height/self.yourview.frame.size.width) können Sie jeden Gleitkommawert verwenden. Danke.

Swift Bewertung: 3.0 -

self.yourview!.translatesAutoresizingMaskIntoConstraints = false 
self.yourview!.addConstraint(NSLayoutConstraint(item: self.yourview!, 
              attribute: NSLayoutAttribute.height, 
              relatedBy: NSLayoutRelation.equal, 
              toItem: self.yourview!, 
              attribute: NSLayoutAttribute.width, 
              multiplier: self.yourview.frame.size.height/self.yourview.frame.size.width, 
              constant: 0)) 
+0

Sein geben mir diesen Fehler beenden app aufgrund zur nicht abgefangenen Ausnahme 'NSInternalInconsistencyException', Grund: 'Multiplikator ist nicht endlich! Das ist illegal. Multiplikator: nan ' – IRD

+0

Wenn Sie nur diese Einschränkung setzen, wird es nicht funktionieren. Sie müssen auch andere Ausrichtungsbeschränkungen festlegen. und Sie können einen Gleitkommawert im Multiplikator angeben.Wenn deine Ansichtsbreite 200 ist und die Höhe 100 ist, wird der Multiplikator 0,5 sein. –

+0

Ich verstehe nicht, wie der Multiplikator funktioniert. Wie geht das? – IRD

6

Sie können "Höhenbeschränkung" bei Design-Zeit im Interface Builder gesetzt. Aktivieren Sie einfach "Entfernen zur Build-Zeit", und es wird entfernt, wenn App läuft.

enter image description here Danach können Sie die Einschränkung "Aspect Ratio" hinzufügen, zum Beispiel in viewDidLoad Methode.

In Xamain.iOS für "16: 9" es sieht wie folgt aus:

this.ImageOfTheDay.AddConstraint(NSLayoutConstraint.Create(
      this.ImageOfTheDay, 
      NSLayoutAttribute.Height, 
      NSLayoutRelation.Equal, 
      this.ImageOfTheDay, 
      NSLayoutAttribute.Width, 
      9.0f/16.0f, 
      0)); 

Oder, wie Mahesh Agrawal sagte:

[self.ImageOfTheDay addConstraint:[NSLayoutConstraint 
           constraintWithItem:self.ImageOfTheDay 
           attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.ImageOfTheDay 
           attribute:NSLayoutAttributeWidth 
           multiplier:(9.0f/16.0f) 
           constant:0]]; 
9

Swift 3:

yourView.addConstraint(NSLayoutConstraint(item: yourView, 
              attribute: .height, 
              relatedBy: .equal, 
              toItem: yourView, 
              attribute: .width, 
              multiplier: 9.0/16.0, 
              constant: 0)) 
+1

Für iOS 8 und höher sollten Sie die isActive-Eigenschaft von NSLayoutConstraint anstelle von addConstraint (:) verwenden. 'aspectRatioConstraint.isActive = true' – dvdblk

2

Ich habe alle Antworten oben versucht, aber nicht funktioniert, und das ist meine Lösung mit schnellen 3:

let newConstraint = NSLayoutConstraint(item: yourView, attribute: .width, relatedBy: .equal, toItem: yourView, attribute: .height, multiplier: 560.0/315.0, constant: 0) 
yourView.addConstraint(newConstraint) 
NSLayoutConstraint.activate([newConstraint]) 
NSLayoutConstraint.deactivate(yourView.constraints) 
yourView.layoutIfNeeded() 
7

Layout Anchors ist die bequemste Möglichkeit, Einschränkungen programmgesteuert festzulegen.

Sagen Sie bitte 5 setzen wollen: 1 Seitenverhältnis für den Button, dann sollten Sie verwenden:

button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true 

Hier ist der vollständige Code:

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let button = UIButton(type: .custom) 
     button.setTitle("Login", for: .normal) 
     button.backgroundColor = UIColor.darkGray 

     self.view.addSubview(button) 

     button.translatesAutoresizingMaskIntoConstraints = false 

     let margins = view.layoutMarginsGuide 

     button.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 20.0).isActive = true 
     button.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: -20.0).isActive = true 
     button.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -20.0).isActive = true 
     button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true 
    } 

} 

Here're mit Code oben geschrieben erzielten Ergebnisse . Sie können sehen, dass, um in das 5 hält: 1 Seitenverhältnis auf verschiedenen Geräten:

Result view

Verwandte Themen