why my aspect ratio constraint is not activated when it is set programatically?

add height constraint programmatically swift
aspect ratio ios tutorial
set aspect ratio constraint programmatically android
uiview dynamic height autolayout programmatically
constraint priority swift
xcode constraints
constraint items must each be a view or layout guide

I simplify my problem to be like just an UIView on ViewController like the image below:

I drag the aspect ratio constraint from storyboard to ViewController code, and named aspectRatioConstraint.

I want that purple view will have the aspect ratio 1:1 when I run the app, so in viewDidLoad I set aspectRatioConstraint.constant = 1 (before running the aspect ratio is 343:128)

but nothing happened when I run the app. the aspect ration doesn't change to 1:1, what went wrong in here ?

As the multiplayer is get only property we can't change its value instead we can change the constant property.

open var multiplier: CGFloat { get }

From the constraints menu:

First Item = (Multiplier * Second Item) + Constant

View.Height = ((128/343) * View.Width) + 0

In other words, 343 = (0.373 * 128) + x

As multiplier has to be changed from 0.373 to 1

(View.Height - constant) / View.width = Multiplier

(343 - x) / 128 = 1

x = 343 - 128

x = 215

aspectRatioConstraint.constant = 215

Interface Builder intelligently selects the set of constraints based on the items Interface Builder does not filter the list of possible constraints based on the gesture's direction. The Aspect Ratio constraint also uses the item's current aspect ratio; Finally, when programmatically creating constraints to the  The most convenient and fluent way to set constraints is using Layout Anchors. It's just one line of code to change aspect ratio for your ImageView. imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1.0/2.0).isActive = true To avoid "[LayoutConstraints] Unable to simultaneously satisfy constraints."

As mentioned, the Aspect Ratio is really the constraint's Multiplier ... which is a "get only" property.

You can use a NSLayoutConstraint extension to change the Multiplier (found here: https://stackoverflow.com/a/33003217/6257435):

extension NSLayoutConstraint {
     Change multiplier constraint

     - parameter multiplier: CGFloat
     - returns: NSLayoutConstraint
    func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint {


        let newConstraint = NSLayoutConstraint(
            item: firstItem,
            attribute: firstAttribute,
            relatedBy: relation,
            toItem: secondItem,
            attribute: secondAttribute,
            multiplier: multiplier,
            constant: constant)

        newConstraint.priority = priority
        newConstraint.shouldBeArchived = self.shouldBeArchived
        newConstraint.identifier = self.identifier

        return newConstraint

Now, in viewDidLoad(), you can call:

aspectRatioConstraint = aspectRatioConstraint.setMultiplier(multiplier: 1.0)

To see real constraints using real code, see Programmatically centerY + 0.0; // Give a view a constant aspect ratio; View.height = 2.0 * View.width + 0.0 When Auto Layout solves these equations, it does not just assign the value of the Assuming the superview's size is already set (for example, the root  If you set app:layout_constraintDimensionRatio="H,3:1" then it means width will be first computed from other constraints and then height will be adjusted according to the aspect ratio. The only problem with your implementation is that you added app:layout_constraintBottom_toBottomOf="parent" to the ImageView, so that it caused app:layout

Need to integrate self.view.layoutIfNeeded() before and after changing the view contstraint constant.

Interface Builder allows to set aspect ratio constraints which comes pretty handy GitHub is home to over 50 million developers working together to host @​formvoltron My normal configuration is to align image view height with text This is not cheap but works fine for small icons making them pretty much  Layout Anchors is the most convenient way to set constraints programmatically. Say you want to set 5:1 aspect ratio for your button then you should use: button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true Here's the full code:

It is a constraint-based layout system that allows developers to create To add the image, click the button at the bottom of the screen not the By default, the Interface Builder is set to preview the UI on iPhone 8 (4.7-inch). Control drag on the image in the Document Outline and select aspect ratio option. If you set a maximum aspect ratio, don't forget to also set android:resizeableActivity false. Otherwise, the maximum aspect ratio has no effect. Otherwise, the maximum aspect ratio has no effect. Note: If your app is not resizable, you should test how it behaves on as many devices as possible.

These are of course based on my experience and varies because of personal Which properties are needed in a layout constraint? When I first started iOS programming in early 2014, I read a book about viewDidLoad is definitely not the recommended way to do manual .multiplier(ratio).constraints() }  For example, the standard high-definition video aspect ratio of 16:9 can be computed as 1920/1080, or 1.7777777778. If this value is a number, the user agent will attempt to obtain media whose aspect ratio is as close as possible to this number given the capabilities of the hardware and the other constraints specified.

Auto Layout Constraints written programmatically in code by making use of Layout I decided to ask my followers with a poll on Twitter which resulted in 851 votes and Either way, we're not in a world yet where we can completely rely on SwiftUI. widthAnchor) ] NSLayoutConstraint.activate(constraints). No constraints on the map frame. There are no limitations on the type of navigation allowed when the map frame is activated. Fixed extent. Sets the map frame to the map extent. Navigation is unavailable when the map frame is activated, and if you resize the map frame the map scale changes to keep the extent.