1

What I'm trying to do:

In a simple UITableView, I've got a form with both text and dates/times that can be edited. I'm using UITextFields in the date/time cells too, so that the inputView property can be used to display a datePicker over the keyboard.

What the problem is:

The problem with this is that these UITextFields are really just dummy fields that show a datePicker when selected. When they're selected, they show this annoying blue cursor that blinks - but these are just dummy text fields, and I don't want them to become visible at all.

If I set alpha = 0 or hidden = YES, then the textField no longer is clickable.

Any ideas of a way around this - or, a different solution to my initial problem? Thanks

Jordan Smith
  • 10,310
  • 7
  • 68
  • 114
  • 1
    Does setting the alpha to `0.01` work? – chown Oct 13 '11 at 03:12
  • Why are you displaying the datePicker over the keyboard as the inputView? Why not provide a button that launches the picker view after hiding the keyboard? – Akshay Oct 13 '11 at 03:15
  • @Akshay because that's not how I designed it to work - I know exactly how I want the interface to be, and by hiding keyboards/clicking extra buttons it's going to make my nice interface yucky :) – Jordan Smith Oct 13 '11 at 03:19
  • It is not really an extra button. I was suggesting replacing the UITextField with the button. However, you can try making the UITextField appear just like its background byt setting its background color, border color, etc., so that it merges with the background and is not visible. – Akshay Oct 13 '11 at 03:22
  • 3
    @chown no, anything 0.02 and above does - Apple decided that invisible (or close to ) UI elements should not be clickable. You can still see it with 0.02 though. – Jordan Smith Oct 13 '11 at 03:22
  • @Akshay the reason why I can't do it with a UIButton is that it doesn't swap in and out the keyboard/datepicker properly - this was my original problem. I've set everything to [UIColor clearColor], but the flashing blue cursor still gets displayed whenever the UITextField is selected. – Jordan Smith Oct 13 '11 at 03:24
  • I doubt whether trying to make a text field invisible is in accordance with the human interface guidelines in that case. What do you expect to happen if the user starts tapping the keyboard keys? – Akshay Oct 13 '11 at 03:31
  • @Akshay There's no keyboard for the hidden text field. It's a date picker, so it doesn't even make sense to have a UITextField. I'm only using it so that a UIDatePicker can be displayed on top of the UIKeyboard - something which is otherwise impossible. – Jordan Smith Oct 13 '11 at 08:53

4 Answers4

3

I finally figured out a way around it. By hiding the UITextView, no touches are detected. However if I overlay a custom UIButton (which can be invisible) then I can activate the UITextField when the button is touched via

[myTextField becomeFirstResponder];

and then the datePicker will replace the keyboard just like it should.

Jordan Smith
  • 10,310
  • 7
  • 68
  • 114
1

I would use a UILabel with a UItapGestureRecognizer to know when the user taps on the label.

The text field control should only be used when actual text input is necessary.

Tell me if you need more help with the gesture recognizer, they are very easy to use and save you a lot of trouble.

Zebs
  • 5,378
  • 2
  • 35
  • 49
  • The point of using a UITextField is that it has an 'inputView' property, solving the problem of a UIKeyboard hiding a UIDatePicker. Otherwise this is what I would have done. – Jordan Smith Oct 13 '11 at 08:51
1

To use the text field's "inputView" property, overlay your textfield on top of a label. Then simply hide and show the textfield from the textFieldDid* methods, but show the actual display value in the label.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    [cell.contentView addSubview:myLabel];
    [cell.contentView addSubview:myTextField];
    ....
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.myTextField) {
        [textField setHidden:YES];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.myTextField) {
        [textField setHidden:NO];
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.myLabel.text = @"Set value here";
}
Steve Spencer
  • 121
  • 1
  • 3
0

See my answer here: Disable blinking cursor in a UITextField

That will hide the cursor and give you the option of whether or not to display anything to the user.

Community
  • 1
  • 1
jcm
  • 1,781
  • 1
  • 15
  • 27