How to set aspect ratio constraint programmatically in ios

I have used auto layout for my ViewControllers. I have set the V and H positions in constraints. but I want to know, How to increase my button size when it change to 5s,6 and 6 plus. This is the way I added constraints for the login button.

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

But my problem is when when it manage the left and right side gap. Its getting strched in iphone 6 and 6 plus since thehight is fixed. How can I increase the size according to the screen size. I think this might be the AspectRatio but how to set the aspect ratio constraint in code.

  • Draw a separator line in conjunction with Auto Layout
  • Setting mask layer on UITableViewCell's subview overrides Auto Layout constraints
  • Using Autolayout Visual Format with Swift?
  • What is the content compression resistance and content hugging of a UIView?
  • Auto layout not set properly when give equal width constrains to buttons
  • Can I disable autolayout for a specific subview at runtime?
  • 4 Solutions Collect From Internet About “How to set aspect ratio constraint programmatically in ios”

    Like this. Try once.

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

    or in the place of (self.yourview.frame.size.height / self.yourview.frame.size.width) you can use any float value.
    Thanks.

    Swift 3:

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

    You can set “Height constraint” at Design-time in Interface Builder.
    Just check ‘”Remove at build time”, and it removes when App will running.

    enter image description here
    After that you can add “Aspect Ratio” constraint, for example, in viewDidLoad method.

    In Xamain.iOS for “16:9” it’s looks like this:

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

    Or, as Mahesh Agrawal said:

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

    I have try all answers above but didn’t work, and this is my solution with swift 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()