Autolayout two views vertically with dynamic height aligned top

swift stack view programmatically
uistackview dynamic height
how to add constraints in xcode 10
how to use auto-layout to move other views when a view is hidden?
xcode constraints
xcode constraints not working
add constraints programmatically swift 4
autolayout percentage position

I have two views that can be dynamic height. Dependent on content either view1 or view2 is the highest.

The highest view should determinate the distance to view3.

In my current implementation view1 and view2 each have a constraint for x pixels for view3. But the problem is view1 and view2 then always will be same height (all the content will not align top)

Try this. Make sure both views 1 and 2, have some constraint to the top of the superview and to the sides (or fixed width, whatever it takes to fix the views in the horizontal direction). Lets say you want that distance to the bottom view to be 30. Give view 1 a constraint to view 3 of =30 with a priority of 200, and another one of >=30 with a priority of 1000 (default). View 2 should just need one constraint to view 3 of >=30 with 1000 priority.

The low priority constraint (of 200) will fix the position of view 3 initially, but is low enough that if view 2 expands, view 3 will move down, and also low enough that the default Content Hugging Priority of view 1 will keep it from expanding its height if it doesn't need to based on its text size.

Auto Layout Guide: Working with Constraints in Interface Builder, Interface Builder provides four Auto Layout to its current fitting size based on its contents. You typically select two or more views before using the Align tool. However, the Horizontally in Container or Vertically in pin the selected item's Leading, Top, Trailing,  So fix a static height for the cell. In right hand side view, Fix top constraint for the ‘orange' label, and bottom constraint for the ‘yellow’ label. So that centre ‘red’ label will get a explicit height which can accommodate one/two lines according to the requirement.

The accepted answer did not work for me (although it did put me on the right path). In my case, view 1 or view 2 could be any size and view 3 had to be below the highest one. This code got it to work, though I must say I find autolayout extremely unintuitive:

let view3TopAnchor1 = statusL.topAnchor.constraint(greaterThanOrEqualTo: view1.bottomAnchor, constant: 10.0)
view3TopAnchor1.priority = UILayoutPriority(100)
view3TopAnchor1.isActive = true

let view3TopAnchor2 = statusL.topAnchor.constraint(greaterThanOrEqualTo: view2.bottomAnchor, constant: 10.0)
view3TopAnchor2.priority = UILayoutPriority(100)
view3TopAnchor2.isActive = true

view1.setContentHuggingPriority(UILayoutPriority(101), for: .vertical)
view2.setContentHuggingPriority(UILayoutPriority(101), for: .vertical)

Auto Layout Guide: Stack Views, To view the source code for these recipes, see the Auto Layout In Interface Builder, start by dragging out a vertical stack view, and add the Top = Top Layout Guide. Finally, in the Size inspector, set the Image View's content-​hugging a complex layout built from multiple layers of nested stack views. So you want something like this: Setting the content size of the scroll view with autolayout is explained in Technical Note TN2154: UIScrollView And Autolayout.To summarize, autolayout sets the contentSize of the scroll view based on the constraints between the scroll view and its descendant views.

float yPos = MAX(view1.frame.size.height,view2.frame.size.height);

view3.frame.origin.y = view1.frame.origin.y + maxHeight;

Proportional Spacing with Auto Layout, It has two rows of image views that are all positioned based on some percentage of the super view width and height. I am using image views so it  Post: We then add our buttons to a Vertical Auto Layout with the other objects in the post. This includes a description, an image and the user's profile. Timeline: We've added three to a Vertical Auto Layout to create our timeline. The Auto Layout Frame is the top-level Frame which sits on the Canvas.

Flexibility using auto layout with nested stack views, augmented with , 2. Add a vertical stack view (1), constrained to the left and right margins, top, and Because the “top” alignment of the stack view (2) sets the top constraints for us, but If the height of the labels to the right could grow taller than the blue view,  autolayout – make height of view relative to half superview height. Questions: have been getting into autolayouts recently and I’m stuck on what seems like a really trivial problem example. I have a view that I want to sit at the top of the screen, and take up half of the screen-height.

Understanding Auto Layout Constraints, Auto Layout got introduced at WWDC 2012, but many developers still fear it and Now top layout guide is a margin that parent view takes up by default which is 20 points in height. Why -20? because margins have width/height of 20 points. This constraint align the vertical center of both the views such that the smaller  If you want to horizontally align a widget to the center of the layout, you need to add two horizontal constraints like below. Similarly if you want to vertically align a view to center, use top and bottom sides in two vertical constraints.

Working with Xcode Auto Layout in Swift and iOS Projects, Auto Layout assists in dynamically calculating the size and position of all the views, Select Single view app and name it AutoLayout. you should see blue lines cutting across the screen both horizontally and vertically. of the top most part of the screen with its text as Welcome to Auto Layout Swift 4.0 . This recipe demonstrates laying out two equal sized buttons. Vertically, the buttons are aligned with the bottom of the screen. Horizontally, they are stretched so that they fill all the available space. Views and Constraints. In Interface Builder, drag two buttons onto the scene. Align them using the guidelines along the bottom of the scene.

Comments
  • Some clarification: When you say highest, do you mean tallest (so the height of view 1 and view 2 can vary independently)? And that the distance from the tallest view (1 or 2) to view 3 should be constant?
  • Yes view1 and view2 height can vary independently. The height from the tallest view(1 or 2) should be a constant to view3.
  • Thanks i was going in the direction of your answer but i did not use two vertical constraints between view 1 and 3.
  • I can't find good documentation of what you're describing. Can you provide a code sample of the constraints you're recommending here? I realize how long it's been since you've answered this, but I'm running into a similar situation and I can't figure out a good solution.
  • @Matt, the written documentation doesn't go into a lot of detail, so you should look at the WWDC 2012 videos (there are 3) concerning auto layout for more information. If this answer doesn't give you enough detail to fix your problem, you should ask a question of your own. I don't really know what code I would show you without more information.
  • Thanks. A few minutes after posting my comment I found the documentation for VFL here. It helped, but wasn't able to answer all my questions.
  • rdelmar, do you think something similar to your answer can be applied to my question, stackoverflow.com/questions/31658526/…?
  • I cant change the frame when using autolayout. I have thought about making custom constraints dependent on the content.