             view.translatesAutoresizingMaskIntoConstraints = false
             addSubview(view)
             
-            constraints.append(view.top(to: previous ?? self, previous?.bottomAnchor ?? topAnchor, offset: offset))
-            constraints.append(view.left(to: self))
-            constraints.append(view.right(to: self))
+            switch axis {
+            case .vertical:
+                constraints.append(view.top(to: previous ?? self, previous?.bottomAnchor ?? topAnchor, offset: offset))
+                constraints.append(view.left(to: self))
+                constraints.append(view.right(to: self))
+                
+                if let lastView = views.last, view == lastView {
+                    constraints.append(view.bottom(to: self))
+                }
+            case .horizontal:
+                constraints.append(view.top(to: self))
+                constraints.append(view.bottom(to: self))
+                constraints.append(view.left(to: previous ?? self, previous?.rightAnchor ?? leftAnchor, offset: offset))
+                
+                if let lastView = views.last, view == lastView {
+                    constraints.append(view.right(to: self))
+                }
+            }
             
             if let width = width {
                 constraints.append(view.width(width))


import UIKit

public enum StackDirection {
    case topToBottom
    case leftToRight
}

public extension UIView {
    
    @discardableResult
    public func stack(views: [UIView], direction: StackDirection = .topToBottom, width: CGFloat? = nil, height: CGFloat? = nil, spacing: CGFloat = 0) -> Constraints {
        
        switch direction {
        case .topToBottom: return stackTopToBottom(views: views, width: width, height: height, spacing: spacing)
        case .leftToRight: return stackLeftToRight(views: views, width: width, height: height, spacing: spacing)
        }
    }
    
    fileprivate func stackTopToBottom(views: [UIView], width: CGFloat? = nil, height: CGFloat? = nil, spacing: CGFloat = 0) -> Constraints {
        
        var offset: CGFloat = 0
        var previous: UIView?

            previous = view
        }
        
        return constraints
    }
}
