PickerView в TextField в TableViewCell в TableViewController

swift
ios

#1

Добрый вечер!

Никак не разберусь почему так получается, но обо всем по порядку. Сначала код проекта:

В AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        
    UIApplication.shared.delegate?.window??.tintColor = .black
    UIApplication.shared.delegate?.window??.rootViewController?.navigationController?.toolbar.isTranslucent = false
    UIApplication.shared.delegate?.window??.rootViewController?.navigationController?.toolbar.tintColor = .black
    
    return true
}

TableViewController.swift:

import UIKit

class TableViewController: UITableViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource  {

    var pickerView: UIPickerView!
    var inputToolBar: UIToolbar!
    
    let cellName = ["first", "second", "third"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.toolbar.isTranslucent = false
        navigationController?.toolbar.backgroundColor = .black
        navigationController?.toolbar.tintColor = .black
        
        tableView.tableFooterView = UIView()
        
        pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.tintColor = .white
        pickerView.backgroundColor = .clear
        
        inputToolBar = UIToolbar()
        inputToolBar.isTranslucent = false
        inputToolBar.tintColor = .white
        inputToolBar.barTintColor = .black
        inputToolBar.backgroundColor = .black

        let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(hide))
        
        inputToolBar.setItems([spacer, done], animated: true)
        inputToolBar.sizeToFit()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellName.count + 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldTableViewCell

        cell.selectionStyle = .none
        cell.textField.delegate = self
        
        if indexPath.row != 3 {
            cell.textLabel?.text = String(indexPath.row + 1)
            cell.textField.inputView = pickerView
            cell.textField.inputAccessoryView = inputToolBar
            cell.textField.inputView?.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        }
        
        cell.textField.keyboardAppearance = .dark

        return cell
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return cellName[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return cellName.count
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        textField.resignFirstResponder()
        return true
    }
    
    @objc func hide() {
        view.endEditing(true)
    }
}

class TextFieldTableViewCell: UITableViewCell {
    
    @IBOutlet weak var textField: UITextField!
}

  1. Не получается установить черный цвет тулбара - ни в didFinishLaunchingWithOptions, ни в ViewDidLoad. Только если установить в storyboard. Что не так?

  2. Почему-то именно в этом проекте цвет шрифта у названий в pickerView черный, хотя в коде настраивается белым (причем в другом проекте показывает белый?!)

  3. Основной вопрос такой. Если перехожу с первого ViewController на TableViewController, где располагаются ячейки таблицы, и нажимаю на текстовое поле, то получаю такой вид:

Но, если вернуться назад, а потом снова к таблице, то уже вижу вот что:

Почему так происходит?? И как сделать, чтобы серый фон у пикера был с самого начала?


#2

Разобрался - надо цвет пикера устанавливать в viewWillLayoutSubviews