通过将 Secure Text Entry 的 UITextView 或 UITextField 的属性设置为 YES,默认情况下不会显示自定义键盘,也无法切换到。
很遗憾,这也会隐藏按键信息,并覆盖和禁用自动大写字母以及自动更正和拼写建议。因此,在强制用户使用 Apple 键盘完成后,您必须切换回 NO。
打开然后关闭此属性可以强制 Apple 键盘成为默认显示的第一个键。
现在,用户仍然可以按全局键,因此您有两个选择:
•一个,你可以让他们检测他们是否使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:@"UITextInputCurrentInputModeDidChangeNotification"
object:nil];,然后重新循环上面的secureTextEntry代码并强制切换回Apple默认键盘(不专业的方法,但很容易编程)。 (*注意!这也将阻止用户使用听写键盘(单击空格键旁边的麦克风图标按钮),除非您使用未记录的代码来检测它是否是听写(确实存在并且应该已经通过Apple 验证了几个帐户。有关此信息可以找到 here)
要么
•二:使用 UIWindows 获取默认键盘的 ONTOP 并添加一个 UIWindow,其中 userInteractionEnabled 设置为 YES 覆盖该键所在的位置(这将需要一些条件语句来确保您覆盖正确的“更改键盘键”以应对各种可能性。(即横向键盘 iPhone4、纵向键盘 iPhone5 等)。
这里有一些演示代码,虽然它适用于纵向键盘(iphone5 和 iphone4)
viewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate> {
UITextField *theTextField;
UIWindow *statusWindow;
}
@end
viewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
theTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 50, 250)];
[theTextField becomeFirstResponder];
theTextField.secureTextEntry = YES;//turn back OFF later (like in `viewDidAppear`) and reset textField properties to YES (like auto correct, auto caps, etc).
theTextField.delegate = self;
[self.view addSubview:theTextField];
//UIWindow *statusWindow; MUST be defined in .h file!
statusWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
statusWindow.frame = CGRectMake(37, self.view.frame.size.height-47, 45, 45);
statusWindow.windowLevel = UIWindowLevelStatusBar;
statusWindow.hidden = NO;
statusWindow.backgroundColor = [UIColor clearColor];
UIButton *keyboardCover = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
keyboardCover.backgroundColor = [UIColor redColor];
[statusWindow addSubview:keyboardCover];
//statusWindow.alpha = 1.00;
statusWindow.userInteractionEnabled = YES;
[statusWindow makeKeyAndVisible];
}
-(void)viewDidAppear:(BOOL)animated {
[theTextField resignFirstResponder];
theTextField.secureTextEntry = NO;
theTextField.autocorrectionType = 2;//ON
theTextField.autocapitalizationType = 2;//ON
theTextField.spellCheckingType = 2;//ON
[theTextField becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是该代码的示例...我使用的是自定义键盘,当我启动应用程序时,它强制我使用 Apple 键盘,然后在“更改键盘”按钮上放置一个红色方块,这使得我无法单击按钮来更改键盘。 (当然,红色方块可以更改为任何东西,例如空白键或处于褪色(禁用)状态的地球图标。)