swift Xcode:以编程方式获取特定约束

46qrfjad  于 2023-05-16  发布在  Swift
关注(0)|答案(2)|浏览(133)

我以编程方式将一个childVC(DrawerViewController)内置到我的mainVC(HomeController)中,现在我需要topAnchor引用。
我知道我可以用IBOutlets引用单个约束并命名它们,但我想学习如何在没有故事板的情况下以编程方式构建东西。现在,我很难通过编程方式正确引用子容器的topAnchor,以便进一步使用此var。
作为一个IBO,它将完美地工作如下:

@IBOutlet private var containerViewTopConstraint: NSLayoutConstraint!

因此,我需要以编程方式引用我的topAnchor并将其命名为containerViewTopConstraint。以编程方式嵌入的childVC的代码:

let drawerViewController = DrawerViewController()

func addDrawer() {
    addChild(drawerViewController)
    view.addSubview(drawerViewController.view)
    drawerViewController.didMove(toParent: self)
    addDrawerConstraints()
}

func addDrawerConstraints() {
    drawerViewController.view.translatesAutoresizingMaskIntoConstraints = false
    drawerViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
    drawerViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
    drawerViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
    drawerViewController.view.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true
}

override func viewDidLoad() {
    super.viewDidLoad()

    addDrawer()
}
ibrsph3r

ibrsph3r1#

我是这么做的。它涉及两件事。首先,设置约束,使其 * 始终 * 相同,设置isActive = true。然后,对于一个(或多个),根据需要激活/停用:

let drawer = UIView()
var drawerOut = [NSLayoutConstraint]()
var drawerIn = [NSLayoutConstraint]()

// place in viewDidLoad()

drawer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
drawer.widthAnchor.constraint(equalToConstant: 526).isActive = true
drawer.heightAnchor.constraint(equalToConstant: 374).isActive = true

drawerIn.append(drawer.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 526))
drawerOut.append(drawer.topAnchor.constraint(equalTo: safeAreaView.bottomAnchor, constant: -370))
NSLayoutConstraint.activate(drawerIn)

// functions to open/close drawer

func openDrawer() {
    NSLayoutConstraint.deactivate(drawerIn)
    NSLayoutConstraint.activate(drawerOut)
    UIView.animate(withDuration: 0.3) { self.view.layoutIfNeeded() }
}

func closeDrawer() {
    NSLayoutConstraint.deactivate(drawerOut)
    NSLayoutConstraint.activate(drawerIn)
    UIView.animate(withDuration: 0.3) { self.view.layoutIfNeeded() }
}

UIView.animate(withDuration:)为所有内容添加了一个很好的动画。
编辑:是的,你可以命名约束并更新它们。有关此的示例-更改heightAnchor常数-请参阅此答案。我认为这个问题是关于如何 * 不 * 使用IB的约束,因为它是关于 * 改变 * 一个约束。

7bsow1i6

7bsow1i62#

你说:
作为一个IBO,它将完美地工作如下:

@IBOutlet private var containerViewTopConstraint: NSLayoutConstraint!

所以我需要引用我的topAnchor程序并将其命名为containerViewTopConstraint。
是的,就这么做吧。您拥有:

drawerViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true

放:

containerViewTopConstraint = drawerViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20)
containerViewTopConstraint.isActive = true

相关问题