开发者

iPhone: How can I make a UITextField invisible but still clickable?

开发者 https://www.devze.com 2023-04-13 03:51 出处:网络
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

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 in开发者_StackOverflowitial problem? Thanks


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.


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.


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";
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号