How to disable UITextField editing but still accept touch?

后端 未结 15 2292
误落风尘
误落风尘 2020-11-29 19:55

I\'m making a UITextField that has a UIPickerView as inputView. Its all good, except that I can edit by copy, paste, cut and select te

相关标签:
15条回答
  • 2020-11-29 20:00

    Not completely sure about how hacky this is but the only thing that did the trick in my case was adding a target action and calling endEditing. Since my picker controls my UITextField.text value, I could dismiss the keyboard as soon as the user clicks on the field. Here's some code:

    uiTextFieldVariable.addTarget(self, action: #selector(dismissKeyboard), for: .editingDidBegin)
    
    @objc private func dismissKeyboard() {
        endEditing(true)
    }
    
    0 讨论(0)
  • 2020-11-29 20:01

    Make your inputView be presented by an hidden textfield which also change the text of the presented and disabled one.

    0 讨论(0)
  • 2020-11-29 20:02

    I used the solution provided by MrMage. The only thing I'd add is you should resign the UITextView as first responder, otherwise you're stuck with the text selected.

    Here's my swift code:

    class TouchableTextView : UITextView {
    
        override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
            self.resignFirstResponder()
            return false
        }
    
        override func shouldChangeTextInRange(range: UITextRange, replacementText text: String) -> Bool {
            self.resignFirstResponder()
            return false
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 20:03

    This worked for me [textview setEditable:NO]; The above answers are overcomplicating the situation.

    0 讨论(0)
  • 2020-11-29 20:10

    For an alternative that handles the UIPickerView and Action Sheets, checkout ActionSheetPicker

    https://github.com/TimCinel/ActionSheetPicker

    It's cocoapods enabled. It handles all of the cancel and done buttons on the Action Sheet. The examples within the sample project are great. I choose the ActionSheetStringPicker, which handles easily just String based options, but the API can handle most anything that I can think of.

    I originally started a solution much like the checkmarked answer, but stumbled onto this project and took me roughly 20 minutes to get things integrated into my app for usage including using cocopods: ActionSheetPicker (~> 0.0)

    Hope this helps.

    Download the git project and look at the following classes:

    • ActionSheetPickerViewController.m
    • ActionSheetPickerCustomPickerDelegate.h

    Here is roughly most of the code that I added, plus the *.h imports.

    - (IBAction)gymTouched:(id)sender {
          NSLog(@"gym touched");
    
          [ActionSheetStringPicker showPickerWithTitle:@"Select a Gym" rows:self.locations initialSelection:self.selectedIndex target:self successAction:@selector(gymWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
     }
    
    
    - (void)actionPickerCancelled:(id)sender {
        NSLog(@"Delegate has been informed that ActionSheetPicker was cancelled");
    }
    
    
    - (void)gymWasSelected:(NSNumber *)selectedIndex element:(id)element {
        self.selectedIndex = [selectedIndex intValue];
    
        //may have originated from textField or barButtonItem, use an IBOutlet instead of element
        self.txtGym.text = [self.locations objectAtIndex:self.selectedIndex];
    }
    
    
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        return NO;  // Hide both keyboard and blinking cursor.
    }
    
    0 讨论(0)
  • 2020-11-29 20:11

    It would be more elegant to create a custom subclass of UITextField that returns NO for all calls to canPerformAction:withSender: (or at least where action is @selector(cut) or @selector(paste)), as described here.

    In addition, I'd also implement - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string as per Nick's suggestion in order to disable inputting text from Bluetooth keyboards.

    0 讨论(0)
提交回复
热议问题