gpt4 book ai didi

objective-c - 向 ViewController 辅助 UIView 添加约束

转载 作者:行者123 更新时间:2023-11-30 12:33:44 25 4
gpt4 key购买 nike

我有一个 Storyboard 设置,其中有一个对话框,该对话框使用辅助 UIView(屏幕截图中的黄色)。这是通过将 UIView 从对象库中拖动到 Storyboard 上的 First Responder 和 Exit 之间来完成的。

我不知道如何向相对于 super View 的辅助 UIView 添加基本约束。右键单击并拖动不会让我应用约束。这可能吗?我需要以编程方式处理所有自动布局/约束吗?

我只需要添加前导/尾随的约束。高度将是固定的,但可能会根据屏幕尺寸进行相应调整。

Secondary view.

最佳答案

这个问题已经解决了。您不能在 IB 中对这些 View 使用右键单击和拖动约束。为什么??因为它们还不是 View 层次结构的一部分! (我傻了..)

在上面带有黄色辅助 View 的示例中,以下是如何使用 VFL(视觉格式语言)对其应用自动布局的方法。

在 ViewDidLoad 中,您首先要关闭 View 自动调整大小掩码:

self.yellowSecondaryView.translatesAutoresizingMaskIntoConstraints = false

现在找到要添加 subview 的位置。假设您使用按钮操作来触发它...在该操作中,您首先添加 View (1),然后添加约束 (2):

// 1
view.addSubview(yellowSecondaryView)

// Create a string representation of the view to use it with VFL.
let viewsDictionary: [String: UIView] = ["yellowSecondaryView": yellowSecondaryView]

//2
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-50-[yellowSecondaryView]-50-|", options: [], metrics: nil, views: viewsDictionary)) // Width of yellow view will be full width, minus 50 points from the LEFT and the RIGHT of the superview (|)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[yellowSecondaryView(250)]", options: [], metrics: nil, views: viewsDictionary)) // Set a static height of 250 for the yellow view

view.addConstraint(NSLayoutConstraint(item: yellowSecondaryView, attribute: .centerY, relatedBy: NSLayoutRelation(rawValue: 0)!, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)) // Center the view vertically in the superview. (credit to @rdelmar in this thread: http://stackoverflow.com/questions/21494039/using-autolayout-in-ib-how-do-i-position-a-uiview-in-the-center-of-the-screen-pr/21494707#21494707)

希望这可以帮助人们创建一个更易于管理和组织的 Storyboard! :)

关于objective-c - 向 ViewController 辅助 UIView 添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43133742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com