有人可以向我解释为什么我的 UITextView 没有显示任何内容吗

Can someone please explain to me why my UITextView isn't showing anything

我是一名努力学习的高中生Swift。有人可以向我解释为什么我的 UITextView 没有显示任何内容。我一直在尝试 HOURS 来解决这个问题。

class ViewController: UIViewController {
    
    var item: UITextField!
    var itemName: UILabel!
    var quantity: UITextField!
    var quantityName: UILabel!
    var listTitle: UILabel!
    var list: UITextView!
    var addButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        view.backgroundColor = .white
                
        setupViews()
        setupConstraints()
    }
    
    func setupViews() {

        item = UITextField()
        item.placeholder = "Placeholder"
        item.textColor = .black
        item.textAlignment = .center
        item.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        item.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(item)
        
        itemName = UILabel()
        itemName.text = "Grocery Item: "
        itemName.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        itemName.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(itemName)
        
        quantity = UITextField()
        quantity.placeholder = "Placeholder"
        quantity.textColor = .black
        quantity.textAlignment = .center
        quantity.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        quantity.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(quantity)
        
        quantityName = UILabel()
        quantityName.text = "Quantity: "
        quantityName.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        quantityName.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(quantityName)
        
        listTitle = UILabel()
        listTitle.text = "Grocery List: "
        listTitle.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        listTitle.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(listTitle)
        
        list = UITextView()
        list.text = "William Henry Gates III (born October 28, 1955) is an American business magnate, software developer, and philanthropist. He is best known as the co-founder of Microsoft Corporation.[2][3] During his career at Microsoft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s.Born and raised in Seattle, Washington, Gates co-founded Microsoft with childhood friend Paul Allen in 1975, in Albuquerque, New Mexico; it went on to become the world's largest personal computer software company.[4][a] Gates led the company as chairman and CEO until stepping down as CEO in January 2000, but he remained chairman and became chief software architect.[7] During the late 1990s, Gates had been criticized for his business tactics, which have been considered anti-competitive. This opinion has been upheld by numerous court rulings.[8] In June 2006, Gates announced that he would be transitioning to a part-time role at Microsoft and full-time work at the Bill & Melinda Gates Foundation, the private charitable foundation that he and his wife, Melinda Gates, established in 2000.[9] He gradually transferred his duties to Ray Ozzie and Craig Mundie.[10] He stepped down as chairman of Microsoft in February 2014 and assumed a new post as technology adviser to support the newly appointed CEO Satya Nadella.[11] In March 2020, Gates left his board positions at Microsoft and Berkshire Hathaway to focus on his philanthropic endeavors including climate change, global health and development, and education.[12]Since 1987, he has been included in the Forbes list of the world's wealthiest people.[13][14] From 1995 to 2017, he held the Forbes title of the richest person in the world all but four of those years.[1] In October 2017, he was surpassed by Amazon founder and CEO Jeff Bezos, who had an estimated net worth of US.6 billion compared to Gates's net worth of US.9 billion at the time.[15] As of August 2020, Gates had an estimated net worth of US3.7 billion, making him the second-wealthiest person in the world, behind Bezos.[16][b]Later in his career and since leaving day-to-day operations at Microsoft in 2008, Gates has pursued a number of philanthropic endeavors. He has given sizable amounts of money to various charitable organizations and scientific research programs through the Bill & Melinda Gates Foundation, reported to be the world's largest private charity.[18] In 2009, Gates and Warren Buffett founded The Giving Pledge, whereby they and other billionaires pledge to give at least half of their wealth to philanthropy"
        list.translatesAutoresizingMaskIntoConstraints = false
        list.isEditable = false
        view.addSubview(list)
        
        addButton = UIButton()
        addButton.setTitle("Add Item", for: .normal)
        addButton.setTitleColor(.blue, for: .normal)
        addButton.translatesAutoresizingMaskIntoConstraints = false
        addButton.layer.borderWidth = 1
        addButton.layer.borderColor = UIColor.blue.cgColor
        addButton.addTarget(list, action: #selector(add), for: .touchUpInside)
        view.addSubview(addButton)

    }
    
    func setupConstraints() {
        NSLayoutConstraint.activate([
            item.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30),
            item.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            itemName.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30),
            itemName.trailingAnchor.constraint(equalTo: item.leadingAnchor, constant: -5)
        ])
        
        NSLayoutConstraint.activate([
            quantity.topAnchor.constraint(equalTo: item.bottomAnchor, constant: 13),
            quantity.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            quantityName.topAnchor.constraint(equalTo: item.bottomAnchor, constant: 13),
            quantityName.trailingAnchor.constraint(equalTo: item.leadingAnchor, constant: -5)
        ])
        
        NSLayoutConstraint.activate([
            addButton.topAnchor.constraint(equalTo: quantityName.bottomAnchor, constant: 20),
            addButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            listTitle.topAnchor.constraint(equalTo: addButton.bottomAnchor, constant: 50),
            listTitle.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            list.topAnchor.constraint(equalTo: listTitle.bottomAnchor, constant: 30),
            list.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
    }
    
    @objc func add() {
        
    }


}

您是否可以为您的 UI 使用故事板设置并使用 @IBOutlet 来引用 UI 实例?

例如

@IBOutlet var Button: NSButton!

我不知道 iOS 是否与 MacOS 相同,但请尝试一下。 (按住控件并将 UI 实例拖入 ViewController class)

您可以设置文本字段的 .borderStyle and/or .backgroundColor

您的文本视图需要宽度和高度。

这是您更正后的代码:

class GrayTextFieldViewController: UIViewController {
    
    var itemTextField: UITextField!
    var itemNameLabel: UILabel!
    var quantityTextField: UITextField!
    var quantityNameLabel: UILabel!
    var listTitleLabel: UILabel!
    var listTextView: UITextView!
    var addButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        view.backgroundColor = .white
        
        setupViews()
        setupConstraints()
    }
    
    func setupViews() {
        
        itemTextField = UITextField()

        // rounded-rect border
        itemTextField.borderStyle = .roundedRect
        // very light-gray background
        itemTextField.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        
        itemTextField.placeholder = "Placeholder"
        itemTextField.textColor = .black
        itemTextField.textAlignment = .center
        itemTextField.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        itemTextField.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(itemTextField)
        
        itemNameLabel = UILabel()
        itemNameLabel.text = "Grocery Item: "
        itemNameLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        itemNameLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(itemNameLabel)
        
        quantityTextField = UITextField()

        // rounded-rect border
        quantityTextField.borderStyle = .roundedRect
        // very light-gray background
        quantityTextField.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        
        quantityTextField.placeholder = "Placeholder"
        quantityTextField.textColor = .black
        quantityTextField.textAlignment = .center
        quantityTextField.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        quantityTextField.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(quantityTextField)
        
        quantityNameLabel = UILabel()
        quantityNameLabel.text = "Quantity: "
        quantityNameLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        quantityNameLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(quantityNameLabel)
        
        listTitleLabel = UILabel()
        listTitleLabel.text = "Grocery List: "
        listTitleLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        listTitleLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(listTitleLabel)
        
        listTextView = UITextView()
        listTextView.text = "William Henry Gates III (born October 28, 1955) is an American business magnate, software developer, and philanthropist. He is best known as the co-founder of Microsoft Corporation.[2][3] During his career at Microsoft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s.Born and raised in Seattle, Washington, Gates co-founded Microsoft with childhood friend Paul Allen in 1975, in Albuquerque, New Mexico; it went on to become the world's largest personal computer software company.[4][a] Gates led the company as chairman and CEO until stepping down as CEO in January 2000, but he remained chairman and became chief software architect.[7] During the late 1990s, Gates had been criticized for his business tactics, which have been considered anti-competitive. This opinion has been upheld by numerous court rulings.[8] In June 2006, Gates announced that he would be transitioning to a part-time role at Microsoft and full-time work at the Bill & Melinda Gates Foundation, the private charitable foundation that he and his wife, Melinda Gates, established in 2000.[9] He gradually transferred his duties to Ray Ozzie and Craig Mundie.[10] He stepped down as chairman of Microsoft in February 2014 and assumed a new post as technology adviser to support the newly appointed CEO Satya Nadella.[11] In March 2020, Gates left his board positions at Microsoft and Berkshire Hathaway to focus on his philanthropic endeavors including climate change, global health and development, and education.[12]Since 1987, he has been included in the Forbes list of the world's wealthiest people.[13][14] From 1995 to 2017, he held the Forbes title of the richest person in the world all but four of those years.[1] In October 2017, he was surpassed by Amazon founder and CEO Jeff Bezos, who had an estimated net worth of US.6 billion compared to Gates's net worth of US.9 billion at the time.[15] As of August 2020, Gates had an estimated net worth of US3.7 billion, making him the second-wealthiest person in the world, behind Bezos.[16][b]Later in his career and since leaving day-to-day operations at Microsoft in 2008, Gates has pursued a number of philanthropic endeavors. He has given sizable amounts of money to various charitable organizations and scientific research programs through the Bill & Melinda Gates Foundation, reported to be the world's largest private charity.[18] In 2009, Gates and Warren Buffett founded The Giving Pledge, whereby they and other billionaires pledge to give at least half of their wealth to philanthropy"
        listTextView.translatesAutoresizingMaskIntoConstraints = false
        listTextView.isEditable = false
        
        view.addSubview(listTextView)
        
        addButton = UIButton()
        addButton.setTitle("Add Item", for: .normal)
        addButton.setTitleColor(.blue, for: .normal)
        addButton.translatesAutoresizingMaskIntoConstraints = false
        addButton.layer.borderWidth = 1
        addButton.layer.borderColor = UIColor.blue.cgColor
        addButton.addTarget(listTextView, action: #selector(add), for: .touchUpInside)
        view.addSubview(addButton)
        
    }
    
    func setupConstraints() {
        NSLayoutConstraint.activate([
            itemTextField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30),
            itemTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            //itemName.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30),
            // set label Base Line anchor equal to textField BaseLine
            itemNameLabel.firstBaselineAnchor.constraint(equalTo: itemTextField.firstBaselineAnchor),
            itemNameLabel.trailingAnchor.constraint(equalTo: itemTextField.leadingAnchor, constant: -5)
        ])
        
        NSLayoutConstraint.activate([
            quantityTextField.topAnchor.constraint(equalTo: itemTextField.bottomAnchor, constant: 13),
            quantityTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            //quantityName.topAnchor.constraint(equalTo: item.bottomAnchor, constant: 13),
            // set label Base Line anchor equal to textField BaseLine
            quantityNameLabel.firstBaselineAnchor.constraint(equalTo: quantityTextField.firstBaselineAnchor),
            quantityNameLabel.trailingAnchor.constraint(equalTo: itemTextField.leadingAnchor, constant: -5)
        ])
        
        NSLayoutConstraint.activate([
            addButton.topAnchor.constraint(equalTo: quantityNameLabel.bottomAnchor, constant: 20),
            addButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            listTitleLabel.topAnchor.constraint(equalTo: addButton.bottomAnchor, constant: 50),
            listTitleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
        
        NSLayoutConstraint.activate([
            listTextView.topAnchor.constraint(equalTo: listTitleLabel.bottomAnchor, constant: 30),
            listTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            
            // textView needs width and height
            listTextView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor, multiplier: 0.9),
            listTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20.0),
        ])
        
    }
    
    @objc func add() {
        
    }
    
}

提示:为您的变量起个更好的名字...quantity 并不意味着它是一个文本字段,而 quantityTextField 则清晰明了。

这是输出: