Tony
Tony

Reputation: 3068

Is it possible to have a UITextField selected without a keyboard present?

Summary of my situation

My app uses a barcode scanner that acts as an external keyboard. While on a particular view I want to have a hidden textfield selected without having any keyboard present. Then I can have the barcode scanner send data to this textfield.

I am using an IPad Air 2.

What I have done so far

I have set the text fields inputView and inputAccessoryView to empty UIViews.

_barcodeText.inputView = [[UIView alloc] initWithFrame:CGRectZero];
_barcodeText.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];

This mostly works, however it leaves this top part of the keyboard: enter image description here

I have tried solving this by removing the UIRemoteKeyboardWindow within the textfields delegate methods, but this solution seems to be very inconsistent and might be causing more problems in other parts of my app. The code I'm using to do this is as follows:

// LOOP THROUGH WINDOWS
for(NSObject *window in [[UIApplication sharedApplication] windows]){
        
    // GET CLASS NAME
    NSString *name = NSStringFromClass([window class]);
        
    // CHECK FOR KEYBOARD WINDOW
    if([name isEqualToString:@"UIRemoteKeyboardWindow"]){
            
        // REMOVE KEYBOARD
        [(UIWindow*)window removeFromSuperview];
        [(UIWindow*)window setHidden:true];
            
    }
        
}

Upvotes: 1

Views: 507

Answers (3)

Derek
Derek

Reputation: 3016

Unfortunately you cannot fully get rid of the keyboard. What I have done is used Sockets barcode scanner, Linked here : https://www.socketmobile.com/home/?languageRedirect=true

They have an SDK that sends data through bluetooth and the keyboard is left alone to do whatever you want with it.

Upvotes: 0

guest
guest

Reputation: 531

in UIViewController viewDidLoad() set keyboard observer:

NotificationCenter.default.addObserver(self,
                                           selector: #selector(keyboardShow),
                                           name: .UIKeyboardWillShow, object: nil)

    func keyboardShow(){
    for w in UIApplication.shared.windows {
        let x = NSStringFromClass(w.classForCoder)
            if x == "UIRemoteKeyboardWindow" {
                w.frame.origin.x = -500
                // OR 
               w.frame = CGRect.zero
            }
    }
}

The code above will hide the keyboard outside main window bounds...

Hope this helps

Upvotes: 0

ClayJ
ClayJ

Reputation: 432

If your hidden UITextField will never actually require user input, just make it a hidden UILabel instead, or just store the data in a String variable rather than in any UI object at all.

Upvotes: 1

Related Questions