Swift - 以编程方式在 UIView 中定位项目

Swift - position items inside UIView programmatically

我被困在我的项目中,因为我不知道如何以编程方式在 `UIView 中约束我的项目(UILabelUIImageViewUITableView、...) .

问题是我的 UIView .transforms 通过点击 button。我知道我可以让所有项目都像我 UIView 那样显示。但这意味着我必须为它们中的每一个制作动画,这似乎不太聪明..

那是我的UIView(灰色背景,->底栏可以忽略)

这就是我设置 UIView 的方式:

let wishlistView: UIView = {
    let v = UIView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .darkGray
    v.layer.cornerRadius = 30
    return v
}()
view.addSubview(wishlistView)

wishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 140.0),
wishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
wishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0),
wishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0),

基本上问题是如何限制 UIView 内的项目,这样我就不必 'animate' 每个项目。相反,我只需要 animate/transform 我的 UIView 其他项目就可以了。

我希望我的问题很清楚,我是新来的:)找不到关于该主题的任何内容,感谢您的帮助!

你可以试一试:

import UIKit

class ViewController: UIViewController {

    let wishlistView: UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .darkGray
        v.layer.cornerRadius = 30
        return v
    }()

    let aLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .yellow
        v.text = "This is a Label"
        v.textAlignment = .center
        return v
    }()

    let theButton: UIButton = {
        let v = UIButton()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setTitle("Tap Me", for: .normal)
        v.backgroundColor = .blue
        return v
    }()

    var openConstraint: NSLayoutConstraint!
    var closedConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(theButton)

        view.addSubview(wishlistView)

        wishlistView.addSubview(aLabel)

        openConstraint = wishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 140.0)
        closedConstraint = wishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0)

        openConstraint.priority = .defaultLow
        closedConstraint.priority = .defaultHigh

        NSLayoutConstraint.activate ([

            theButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20.0),
            theButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),

            openConstraint,
            closedConstraint,

            wishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
            wishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0),
            wishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0),

            aLabel.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: 16.0),
            aLabel.leadingAnchor.constraint(equalTo: wishlistView.leadingAnchor, constant: 16.0),
            aLabel.trailingAnchor.constraint(equalTo: wishlistView.trailingAnchor, constant: -16.0),

        ])

        theButton.addTarget(self, action: #selector(showHideWishlist(_:)), for: .touchUpInside)
    }

    @objc func showHideWishlist(_ sender: Any?) -> Void {
        if closedConstraint.priority == .defaultHigh {
            closedConstraint.priority = .defaultLow
            openConstraint.priority = .defaultHigh
        } else {
            openConstraint.priority = .defaultLow
            closedConstraint.priority = .defaultHigh
        }
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }
}

点击 "Tap Me" 按钮将显示/隐藏 "wishlistView":