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

Posted

技术标签:

【中文标题】Swift - 以编程方式在 UIView 中定位项目【英文标题】:Swift - position items inside UIView programmatically 【发布时间】:2019-11-19 16:44:02 【问题描述】:

我被困在我的项目中,因为我不知道如何以编程方式在 `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 内的项目,因此我不必为每个项目“设置动画”。相反,我只需要对我的UIView 进行动画/转换,其余的项目就会随之而来。

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

【问题讨论】:

以相同的方式添加和约束以... wishlistView.addSubview(aLabel) 后跟 aLabel.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: 12.0), 但是如果我移动我的UIView,该项目会随之移动吗?还是会导致错误? 你是对的!我的问题是我仍然有view.addSubview(testLabel) 而不是wishlistView.addSubview(testLabel)。谢谢!! 【参考方案1】:

你可以试试这个:

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”:

【讨论】:

以上是关于Swift - 以编程方式在 UIView 中定位项目的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中以编程方式从 UIView 中删除安全区域?

以编程方式在 UIView 上添加 UITextField Swift

在 UIImage 上添加 UIView - swift - 以编程方式

以编程方式定位 UIView 不起作用 - 坚持情节提要定位

(Swift) 以编程方式在 UITableViewController 上方添加 UIView

Swift:以编程方式附加自定义 UIView