数字键盘键盘在iPhone中不显示返回键

jay*_*dev 11 iphone ios-4.2

我是iPhone技术的新手.我在iPhone应用程序中保存了一个问题,但是当我使用textfield键盘类型数字键盘时,键盘没有显示返回/完成按钮.

在文本字段中输入数字后如何隐藏数字键盘键盘?你有什么解决方案吗?请提供帮助.

Osc*_*car 11

你可以这样做:

@property (nonatomic, strong) UITextField *textField;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonDidPressed:)];
    UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
    [toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
    self.textField.inputAccessoryView = toolbar;
}

- (void)doneButtonDidPressed:(id)sender {
    [self.textField resignFirstResponder];
}

+ (CGFloat)toolbarHeight {
    // This method will handle the case that the height of toolbar may change in future iOS.
    return 44.f;
}
Run Code Online (Sandbox Code Playgroud)

  • 对于iOS 6.1,除非我用如下框架初始化工具栏,否则这不起作用:`UIToolbar*toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,44)] ;`否则,完美答案 (2认同)

Naj*_*hah 6

你必须使用UIKeyboardTypeNumberPad,试试这个而不是UIKeyboardTypeNumbersAndPunctuation,它不仅会显示返回键,还会为你提供一些额外的标点符号