分配给创建视图的函数时不出现 var 约束

var constraints not appearing when assigned to a function that creates a view

我希望我的 swift 代码使用 var 约束来让对象添加到代码中。我在下面添加了我正在寻找的 gif。现在下面的代码可以编译,但是你可以在下面的 gif 中看到的黑色视图没有出现在下面的代码中。在我下面的代码中,黑框根本没有出现。

import UIKit
public class ViewController : UIViewController {
    var slider = UISlider()
    var image1Width2: NSLayoutConstraint!
    var iHieght: NSLayoutConstraint!
    public override func viewDidLoad() {
        super.viewDidLoad()
        slider.value = 0.5
        slider.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(slider)
        slider.isUserInteractionEnabled = true
        slider.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        slider.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        slider.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1).isActive = true
        slider.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1).isActive = true
        
        button.frame = .init(x: self.view.bounds.midX,
                             y: 0,
                             width: 100,
                             height: 100)
        self.view.addSubview(button)
        image1Width2 =  view.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.06)
        iHieght =  view.widthAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.06)
        slider.addTarget(self, action: #selector(increase), for: .allEvents)
    }
    
    private lazy var button: UIButton = {
        let button = UIButton()
        button.backgroundColor = .blue
        button.setTitleColor(.white, for: .normal)
        button.setTitle("add", for: .normal)
        button.addTarget(self,
                         action: #selector(addBlackView),
                         for: .touchUpInside)
        return button
    }()
    
    private func getBlackView() -> UIView {
        let view = UIView()
        view.backgroundColor = .black
     
    
        image1Width2 =  view.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.06)
        iHieght =  view.widthAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.06)
    
        let recognizer = UIPanGestureRecognizer(target: self, action: #selector(moveView(_:)))
        view.addGestureRecognizer(recognizer)
        return view
    }
    
    @objc
    private func addBlackView() {
        let view = getBlackView()
        self.view.addSubview(view)
    }
    
    @objc
    private func moveView(_ recognizer: UIPanGestureRecognizer) {
        switch recognizer.state {
        case .began:
            print("gesture began")
        case .changed:
            let translation = recognizer.translation(in: self.view)

            recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                            y: recognizer.view!.center.y + translation.y)
            recognizer.setTranslation(.zero, in: self.view)
        default:
            break
        }
    }



@objc func increase() {


        image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
    iHieght.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
    






}


}

你在写这个 code.Mainly iOS 基本原则时犯了几个错误 请参考这个修改后的代码,修改它并得到你的结果。

class sampleViewController: UIViewController {

var image1Width2: NSLayoutConstraint!
var iHieght: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white

    view.addSubview(slider)
    slider.translatesAutoresizingMaskIntoConstraints = false
    slider.value = 0.5
    slider.isUserInteractionEnabled = true
    NSLayoutConstraint.activate([
        slider.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        slider.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        slider.heightAnchor.constraint(equalToConstant: 100),
        slider.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
    ])

    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
        button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
        button.widthAnchor.constraint(equalToConstant: 100),
        button.heightAnchor.constraint(equalToConstant: 80),
    ])
    button.addTarget(self,action: #selector(addBlackView),for: .touchUpInside)
    slider.addTarget(self, action: #selector(increase), for: .allEvents)

}

let slider:UISlider = {
    let slider = UISlider(frame: .zero)
    return slider
}()

private lazy var button: UIButton = {
    let button = UIButton()
    button.backgroundColor = .blue
    button.setTitleColor(.white, for: .normal)
    button.setTitle("add", for: .normal)
    return button
}()

let blackView: UIView = {
    let view = UIView()
    view.backgroundColor = .black
    return view
}()

@objc
private func addBlackView() {
    self.view.addSubview(blackView)
    blackView.translatesAutoresizingMaskIntoConstraints = false
    blackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    blackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    image1Width2 = blackView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.1)
    image1Width2.isActive = true
    iHieght = blackView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1)
    iHieght.isActive = true
    view.layoutIfNeeded()

    let recognizer = UIPanGestureRecognizer(target: self, action: #selector(moveView(_:)))
    blackView.addGestureRecognizer(recognizer)
}

@objc private func moveView(_ recognizer: UIPanGestureRecognizer) {
    switch recognizer.state {
    case .began:
        print("gesture began")
    case .changed:
        let translation = recognizer.translation(in: self.view)

        recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                        y: recognizer.view!.center.y + translation.y)
        recognizer.setTranslation(.zero, in: self.view)
    default:
        break
    }
}

@objc func increase() {
    image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
    iHieght.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
}}