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

标签 ios swift uiview constraints

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

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

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

enter image description here

这就是我设置 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 内的项目,这样我就不必为每个项目“设置动画”。相反,我只需要对我的 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()
        }
    }
}

点击“点击我”按钮将显示/隐藏“wishlistView”:

enter image description here

关于ios - Swift - 以编程方式在 UIView 中定位项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58939014/

相关文章:

ios - Xamarin.iOS/Akavache/加密缓存工作示例

ios - 在 iOS 中实现跨 Controller 动画的最佳实践是什么?

iOS 应用传输安全

ios - 如何为非基于文本的内容创建 UIMenuController?

ios - subview 背景颜色没有动画

cocoa-touch - 有没有办法将 UIView 动画与自定义缓动方程一起使用

ios - Storyboard不包含带有标识符的 View Controller

swift - 快速使用委托(delegate)将字符串从一个 View Controller 发送到另一个 View Controller

ios - 如何使用键值将数组与字典分组。

ios - UICollectionView 中的动态单元格高度