Swift: Getting 'Snapshotting a view that has not been rendered..' error when trying to open a URL in safari from my app

前端 未结 8 1594
一整个雨季
一整个雨季 2020-12-28 12:48

One of the specifications of my app is that on tapping a tableView cell, the user will be redirected to the website associated with the cell. Here is the code:



        
8条回答
  •  有刺的猬
    2020-12-28 13:13

    I was facing the similar problem (same warning XD).

    Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.


    My Xcode version is 9.4.1, and everything was perfect until I add resignFirstResponder() in textFieldShouldReturn(_ textField: UITextField).


    Here was my situation:

    When I touched UITextField with text in the UITableViewCell, It will show the warning.

    So I changed the way to trigger UITextField(Edit version). It never show the warning again.

    In conclusion, I still don't know why this happened, but what I can do is avoid the warning, hope It will help someone :D



    Edited code:

    class TableViewCell: UITableViewCell {
        // add this in TableViewCell
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            answerTextField.isEnabled = selected
            if selected {
                answerTextField.becomeFirstResponder()
            } else {
                answerTextField.resignFirstResponder()
            }
        }
    }
    


    Original code:

    class ViewController: UIViewController {
    
        private let tableView: UITableView = {
            let t = UITableView()
            t.separatorStyle = .none
            t.backgroundColor = .clear
            return t
        }()
    
        private let cellId = "Cell"
    
        private let data = [
            Model(title: "A", answer: "a"),
            Model(title: "B", answer: "b"),
            Model(title: "C", answer: nil),
            Model(title: "D", answer: nil)
        ]
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.dataSource = self
            tableView.delegate = self
            tableView.register(TableViewCell.self, forCellReuseIdentifier: cellId)
            view.addSubview(tableView)
            tableView.frame = view.frame
        }
    }
    
    extension ViewController: UITableViewDataSource, UITableViewDelegate {
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 3
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return data.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? TableViewCell {
                cell.contentView.backgroundColor = indexPath.section % 2 == 0 ? .gray : .white
                cell.setData(data[indexPath.row])
                return cell
            }
            return UITableViewCell()
        }
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }
    }
    
    class TableViewCell: UITableViewCell {
    
        private let titleLabel = UILabel()
        private let answerTextField = UITextField()
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            setupViews()
        }
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupViews()
        }
    
        func setupViews() {
            setupTitleLabels()
        }
    
        func setData(_ data: Model) {
            titleLabel.text = data.title
            answerTextField.text = data.answer
        }
    
        private func setupTitleLabels() {
            answerTextField.delegate = self
    
            let sv = UIStackView(arrangedSubviews: [titleLabel, answerTextField])
            sv.axis = .vertical
            sv.spacing = 0
            sv.distribution = .fill
    
            contentView.addSubview(sv)
            sv.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
            sv.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
            sv.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
            sv.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        }
    }
    
    extension TableViewCell: UITextFieldDelegate {
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
    }
    
    struct Model {
        let title: String
        var answer: String?
        init(title: String, answer: String? = nil) {
            self.title = title
            self.answer = answer
        }
    }
    

提交回复
热议问题