Gilbert Tyler
Gilbert Tyler

Reputation: 57

Picker View Row 1 text color issue - Swift

I have been spending too much time on this. I would greatly appreciate your help.

I have a picker view in my iOS app page and I have the following code to change the color of the text of the selected row. However, for some reasons, the row #1 reacts very strangely. I have added a button to the accessory view of the text field that brings up the picker view. When I tap the button, the input view switches to number pad. And if I tap the button again, it would switch back to the picker view. Here is the issue. If I select row #1 of my picker view, the color of the row #1 text changes to blue (as I intended it to be). However, if I tap the button to switch to the number pad mode, and then tap the button to switch back to the picker view, the color of the selected text in row #1 changes back to black.

Please shed some light on me. Thanks.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

      var title = UILabel()
         if let view = view {
                title = view as! UILabel
          }
        title.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
        title.textColor = row == pickerView.selectedRow(inComponent: component) ? .systemBlue : .black
        
        title.text = pickerViewData[row]
        title.textAlignment = .center
        
    return title

    }

and the switch button function is below... only the row #1 is not working as expected... it's really strange... all the other rows change color exactly as intended... even if I use titleForRow along with attributedTitleForRow, I get the same result. Is this a Swift bug..? I tried to search for "picker view row #1 issue", but there doesn't seem to be many questions.

func switchInputView() {

    pickerViewStatus = !pickerViewStatus
    resetInputView()
    pickerView.reloadAllComponents()
}

func resetInputView() {
    switch pickerViewStatus {
    case true:
        activeTextField.inputView = pickerView
       
    case false:
        activeTextField.inputView = nil
        activeTextField.keyboardType = .numberPad
        activeTextField.becomeFirstResponder()
    }
   
    addToolbarForPickerView()
    activeTextField.reloadInputViews()
    
}

Upvotes: 0

Views: 48

Answers (0)

Related Questions