2

How to design the following layout with StackView

enter image description here

I know that it can be easily created without using stackview but is it possible using stackview because i have lots of ui which already designed using stackview how can i add divider between views like above image

For that i set the following constraints

enter image description here

**But run time it shows like ** enter image description here

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44

3 Answers3

3

You can use some simple Autolayout constraints to get this kind of interface with a UIStackView.

The below image describe the hierarchy and constraints that you can apply:

enter image description here

Also the Accept and Reject buttons have Equal Width constraint.

Output screenshot:

enter image description here

PGDev
  • 23,751
  • 6
  • 34
  • 88
2

You can do that using UIStackViews, here is how:

First, create a subclass of UIView to use as the class of the UIView in between the 2 buttons.

class CustomWidthView: UIView {

    override var intrinsicContentSize: CGSize {
        return CGSize(width: 1, height: self.frame.height)
    }
}

You only need to override intrinsicContentSize.

Now, add your 2 buttons and an empty UIView between them, and set the class of the UIView to be CustomWidthView. Embed your 2 buttons and the view between them in a stack view, and set the position of the stack view properly using the appropriate constraints.

Select the stack view and from Attributes inspector, find the property named Distribution and from the drop down menu next to it, select Fill Proportionally.

To reflect your changes in the UI Builder, select the custom width view and go to Size inspector, go down to the bottom of the list and you'll find a property called Intrinsic Size, change its value to be Placeholder, and from the width drop down menu, select 1.

There may be a better way to achieve this, but this is the one I found for now and I'd like to find a better one.

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • You don't need to create any subclasses. You can just do it with constraints on the subviews. [The `UIStackView` documentation](https://developer.apple.com/documentation/uikit/uistackview) explicitly says “You can also fine tune an arranged view’s appearance by adding additional constraints to the arranged view.” – rob mayoff Aug 19 '17 at 08:24
  • I agree that this is overkill, the first solution I tried was similar to the one in your answer, but for some reason, it is drawn as expected in the UI Builder, but when you run, then middle view didn't get displayed. – Mo Abdul-Hameed Aug 19 '17 at 08:31
2

Since UIStackView uses auto layout to arrange its subviews, you can modify its behavior by creating constraints.

I assume you have three views in the stack: the accept button, the divider, and the cancel button.

Create two constraints:

  1. An equal-width constraint between the accept button and the cancel button.

  2. A fixed-width constraint on the divider (presumably setting its width to 1).

See also this Q&A.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848