Ios StackView对齐问题

Ios StackView对齐问题,ios,iphone,xcode,xcode7,uistackview,Ios,Iphone,Xcode,Xcode7,Uistackview,我想用不同的对齐方式对齐3个堆栈 1-图像0-0两条边如下图所示 2-用户信息+文本10-10形成两个边缘 问题是,当我将它们堆叠在一起时,我只能将它们全部设置为0到所有边距,每个堆栈不堆叠 在我的项目中,所有内容都卡在左边的图像中,我希望在用户+文本堆栈中都有空白! 如何修复此问题,使其显示如下图所示 因为我非常喜欢UIStackViews,所以我用你的问题作为实验的理由。您可以将以下内容粘贴到Xcode 7中: import UIKit import XCPlayground let ho

我想用不同的对齐方式对齐3个堆栈

1-图像0-0两条边如下图所示

2-用户信息+文本10-10形成两个边缘

问题是,当我将它们堆叠在一起时,我只能将它们全部设置为0到所有边距,每个堆栈不堆叠

在我的项目中,所有内容都卡在左边的图像中,我希望在用户+文本堆栈中都有空白! 如何修复此问题,使其显示如下图所示


因为我非常喜欢
UIStackView
s,所以我用你的问题作为实验的理由。您可以将以下内容粘贴到Xcode 7中:

import UIKit
import XCPlayground

let hostView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
hostView.backgroundColor = .whiteColor()
XCPlaygroundPage.currentPage.liveView = hostView

let headerLabel = UILabel()
headerLabel.translatesAutoresizingMaskIntoConstraints = false
headerLabel.numberOfLines = 0
headerLabel.text = "This is an info text shown on top of the image. The info text should inform the reader about the topic."

let topStackView = UIStackView(arrangedSubviews: [headerLabel])

let image = UIImage(named: "header.jpg")
let imageView = UIImageView(image: image)
imageView.backgroundColor = .yellowColor()
imageView.contentMode = .ScaleAspectFit

let bottomLabel = UILabel()
bottomLabel.translatesAutoresizingMaskIntoConstraints = false
bottomLabel.numberOfLines = 0
bottomLabel.text = "This is the story text. It is interessting and shows a lot insight to the topic. The reader should be eager to read it and at the end he/she would be able to share with friends and family."

let bottomStackView = UIStackView(arrangedSubviews: [bottomLabel])

let stackView = UIStackView(arrangedSubviews: [topStackView, imageView, bottomStackView])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Vertical
stackView.spacing = 10

hostView.addSubview(stackView)

let views = ["stackView": stackView]
var layoutConstraints: [NSLayoutConstraint] = []
layoutConstraints += NSLayoutConstraint.constraintsWithVisualFormat("|[stackView]|", options: [], metrics: nil, views: views)
layoutConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]", options: [], metrics: nil, views: views)
layoutConstraints.append(headerLabel.leadingAnchor.constraintEqualToAnchor(topStackView.leadingAnchor, constant: 10))
layoutConstraints.append(headerLabel.trailingAnchor.constraintEqualToAnchor(topStackView.trailingAnchor, constant: 10))

let imageSize = image!.size
let imageHeight = hostView.frame.size.width * imageSize.height / imageSize.width
layoutConstraints.append(imageView.heightAnchor.constraintEqualToConstant(imageHeight))

layoutConstraints.append(bottomLabel.leadingAnchor.constraintEqualToAnchor(bottomStackView.leadingAnchor, constant: 10))
layoutConstraints.append(bottomLabel.trailingAnchor.constraintEqualToAnchor(bottomStackView.trailingAnchor, constant: 10))

NSLayoutConstraint.activateConstraints(layoutConstraints)

或者您也可以查看我的“故事视图”页面。

谢谢,但这对我来说是一个小小的进步。我主要是在故事板中完成的,您认为可以使用故事板吗?我在属性检查器中使用了对齐,但它不起作用技巧是使用三个堆栈视图。打开用于顶部标签,打开用于图像和主堆栈视图下方的标签。然后针对相应的堆栈视图向顶部和底部标签添加尾随和前导约束。这样,如果我不将它们全部堆叠在一起,它就不会中断。主堆栈视图的排列视图是顶部堆栈视图、图像视图和底部堆栈视图。堆栈视图的威力来自将堆栈视图放入堆栈视图的能力。你应该看WWDC介绍堆栈视图的演讲。谢谢你,兄弟,它单独工作非常好,只有3个堆栈,我希望以后不会损坏:)