Ios 使用autolayout-SWIFT确定UIView的宽度

Ios 使用autolayout-SWIFT确定UIView的宽度,ios,swift,uiview,autolayout,width,Ios,Swift,Uiview,Autolayout,Width,好的,在interface builder(Main.storyboard)中,我在UIScrollView中嵌入了一个containerView:UIView。在containerView中,我想创建额外的UIView,以保存标题、正文等内容块。这样做的原因是,内容可以垂直滚动,但不能水平滚动 我的目标是使用autolayout创建这些不同的UIView。目前,containerView会根据所用设备的屏幕大小自动调整其宽度,以防止水平滚动。它使用我为宽度约束创建的IBOutlet来实现这一点

好的,在interface builder(
Main.storyboard
)中,我在
UIScrollView
中嵌入了一个
containerView:UIView
。在
containerView
中,我想创建额外的
UIView
,以保存标题、正文等内容块。这样做的原因是,内容可以垂直滚动,但不能水平滚动

我的目标是使用autolayout创建这些不同的
UIView
。目前,containerView会根据所用设备的屏幕大小自动调整其宽度,以防止水平滚动。它使用我为宽度约束创建的IBOutlet来实现这一点。目前看来是这样的:

@IBOutlet var containerView: UIView!
@IBOutlet var containerViewWidthConstraint: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()

    //Prevents horizontal scrolling
    containerViewWidthConstraint.constant = self.view.frame.size.width

    createHeader()
}
然后,我创建了一个名为
createheader{}
的函数,该函数将
headerView:UIView
固定在
containerView
的顶部,并从
containerView
的任意边缘固定8个点:

func createHeader() {
    //Create header
    let headerView = UIView()
    headerView.backgroundColor = UIColor.blueColor()
    self.containerView.addSubview(headerView)

    //Create header constraints
    let leftMargin = NSLayoutConstraint(item: headerView, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1.0, constant: 8)
    let rightMargin = NSLayoutConstraint(item: containerView, attribute: .Trailing, relatedBy: .Equal, toItem: headerView, attribute: .Trailing, multiplier: 1.0, constant: 8)
    let topMargin = NSLayoutConstraint(item: headerView, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1.0, constant: 70)
    let heightConstraint = NSLayoutConstraint(item: headerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 160)

    //Activate header constraints
    headerView.setTranslatesAutoresizingMaskIntoConstraints(false)
    NSLayoutConstraint.activateConstraints([leftMargin,rightMargin,topMargin,heightConstraint])

    println(headerView.frame.size.width)
}
现在,由于
headerView
中内容的大小取决于所用设备的屏幕大小,因此我希望能够创建一些函数,根据
headerView
本身的宽度大小来调整内容的宽度。但是,每次我尝试使用以下方法获取
headerView
的宽度时:

println(headerView.frame.size.width)
它返回的值为零,情况显然不是这样,因为它仍在根据上述约束创建蓝色背景
headerView


为什么斯威夫特没有意识到
headerView
有一个宽度?安装约束后,如果要立即更新框架,则需要调用
layoutifneedd
,如何获取
headerView的宽度

func createHeader() {
    //Create header
    let headerView = UIView()
    headerView.backgroundColor = UIColor.blueColor()
    self.containerView.addSubview(headerView)

    ...

    //Activate header constraints
    headerView.setTranslatesAutoresizingMaskIntoConstraints(false)
    NSLayoutConstraint.activateConstraints([leftMargin,rightMargin,topMargin,heightConstraint])

    self.containerView.layoutIfNeeded() // Updates the frames
    println(headerView.frame.size.width) // Will output the correct width
}

请注意,这将在用户界面循环的下一次迭代中自动发生,但是,如果您希望立即看到效果,这对您没有帮助。

现在,我将回到
containerView
以获取宽度:
(containerView.frame.size.width-16)
。我仍然不明白为什么
headerView
被认为没有宽度……为什么你不在“headerView”中为你的新内容添加限制?这是计划,但是如果
headerView
被认为没有宽度,我怎么能这样做呢?例如,我想在
headerView
中放置一个自动调整大小并居中的方形图像。这是通过使用1:1纵横比约束和两个相等的水平约束来实现的,一个是
.training
,一个是
.Leading
。但是当我去创建与
头视图的宽度相关的水平约束时
headerView.frame.size.width/3.5
,此值一直返回为0.0
func createHeader() {
    //Create header
    let headerView = UIView()
    headerView.backgroundColor = UIColor.blueColor()
    self.containerView.addSubview(headerView)

    ...

    //Activate header constraints
    headerView.setTranslatesAutoresizingMaskIntoConstraints(false)
    NSLayoutConstraint.activateConstraints([leftMargin,rightMargin,topMargin,heightConstraint])

    self.containerView.layoutIfNeeded() // Updates the frames
    println(headerView.frame.size.width) // Will output the correct width
}