【问题标题】:Determining width of a UIView using autolayout - SWIFT使用自动布局确定 UIView 的宽度 - SWIFT
【发布时间】:2015-11-04 01:17:15
【问题描述】:

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

我的目标是使用自动布局来创建这些不同的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

为什么 SWIFT 不识别 headerView 有宽度?以及如何获取headerView 的宽度?

【问题讨论】:

  • 截至目前,我正在与containerView 联系以获取宽度:(containerView.frame.size.width - 16)。我还是不明白为什么headerView 被识别为没有宽度......
  • 你为什么不在'headerView'中为你的新内容添加约束?
  • 这是计划,但如果headerView 被识别为没有宽度,我该怎么办?例如,我想在headerView 中放置一个自动调整大小并居中的方形图像。这是通过使用 1:1 纵横比约束和两个相等的水平约束来完成的,一个 .Trailing 和一个 .Leading。但是当我去创建与headerView 的宽度相关的水平约束时...headerView.frame.size.width / 3.5,这个值一直返回为 0.0

标签: ios swift uiview autolayout width


【解决方案1】:

安装约束后,如果您想立即更新框架,则需要调用layoutIfNeeded

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
}

请注意,这将在 UI 循环的下一次迭代中自动发生,但是当您想立即查看效果时,这对您没有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多