4

I'm using a numeric keyboard for input from a UITextField. I noticed that Interface Builder has a return key property called "Done". When I select "Done" under the return key options, I do not see that a Done button is created on the numeric keyboard. I see the "Done" button on other keyboard types if I change it from a numeric keyboard to something else, but not when using a numeric keyboard.

  • I want to create a "Done" button on the keyboard which will hide the keyboard when pressed
  • I want to do this because of the amount of real estate the keyboard takes up
  • I'm very new to iOS development, so the easiest and least convoluted way to accomplish this goal would be appreciated

Thanks in advance!

4

3 回答 3

8

执行此操作的典型方法是将包含按钮的工具栏分配给inputAccessoryView文本视图的属性。

于 2012-05-21T18:53:12.247 回答
0

我在网上找到了这个解决方案,但这意味着为你的完成按钮制作一个图像:http: //www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

您也可以使用相同的方法来获取示例中显示的 UIKeyboard 视图,并按照@Jim 的建议执行以下操作:

UIToolbar *keyboardViewToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 206, 320, 44)];

UIBarButtonItem *keyboardDoneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneSettingDate)];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"Enter Number" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                         target:nil 
                                                                         action:nil];
UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                         target:nil 
                                                                         action:nil];



NSArray *barButtonItems = [[NSArray alloc] initWithObjects:spacer1, title, spacer2, pickerDoneButton, nil];
  [keyboardViewToolbar setItems:barButtonItems];

并将该视图添加到键盘视图。

另外,作为一个单独的建议,考虑到您刚刚开始。在 iTunes U 上,您可以下载为 iOS 开发应用程序的斯坦福课程,这是一门非常完整的 iOS 开发课程,可在 iTunes Store 上免费获得。

于 2012-05-21T19:07:46.900 回答
0
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        UIToolbar* KeyBoardToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        KeyBoardToolbar.barStyle = UIBarStyleBlackTranslucent;
        KeyBoardToolbar.items = [NSArray arrayWithObjects:
                                 [[UIBarButtonItem alloc]initWithTitle:[GlobalSettings GetTextBySelectedLocale:@"Done"] style:UIBarButtonItemStyleBordered target:self action:@selector(DismissKeyBoard)],
                                 [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                 nil];
        [KeyBoardToolbar sizeToFit];
        txtMessage.inputAccessoryView = KeyBoardToolbar;
    }

txtMessage可能UiTextFieldUITextView.

-(void)DismissKeyBoard{
    [self.view endEditing:YES];
}
于 2016-03-10T13:14:49.957 回答