因此,小鍵盤預設情况下不帶有"完成"或"下一步"按钮,因此我想添加一个.在iOS 6及以下版本中,有一些技巧可以在鍵盤上添加按钮,但它们似乎在iOS 7中不起作用。
首先,我订阅顯示通知的鍵盤
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
然後我尝試在鍵盤顯示時添加一个按钮:
- (void)keyboardWillShow:(NSNotification *)note
{
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
doneButton.frame = CGRectMake(0, 50, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(dismissKeyboard) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
但是for迴圈不会執行,因為它找不到任何子视圖.有什麼建議? 我找不到適用於iOS7的任何解決方案,所以應该采用其他方法吗?
編輯:感谢所有對工具欄人員的建議,但我不愿意走那條路,因為我的空間非常有限(而且很丑陋)。
最新回復
- 5月前1 #
- 5月前2 #
更安全的方法是使用
UIToolBar
与Done
Button asinputAccessoryView
Sample Code :
UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; txtField.inputAccessoryView = keyboardDoneButtonView;
您的
-doneClicked
方法應如下所示:- (IBAction)doneClicked:(id)sender { NSLog(@"Done Clicked."); [self.view endEditing:YES]; }
Sample Code Swift:
let keyboardDoneButtonView = UIToolbar.init() keyboardDoneButtonView.sizeToFit() let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("doneClicked:"))) keyboardDoneButtonView.items = [doneButton] textFieldInput.inputAccessoryView = keyboardDoneButtonView
您的
-doneClicked
方法應如下所示:func doneClicked(sender: AnyObject) { self.view.endEditing(true) }
- 5月前3 #
更簡單的方法:
Swift 3.0 and above :
func addDoneButton() { let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing(_:))) keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar }
Swift 2.3 and below :
func addDoneButton() { let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done, target: view, action: #selector(UIView.endEditing(_:))) keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar }
Objective C :
- (void)addDoneButton { UIToolbar* keyboardToolbar = [[UIToolbar alloc] init]; [keyboardToolbar sizeToFit]; UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.view action:@selector(endEditing:)]; keyboardToolbar.items = @[flexBarButton, doneBarButton]; self.textField.inputAccessoryView = keyboardToolbar; }
編輯:我已经建立了一个名為DCKit的有用的庫,该庫已经具有現成的工具欄:
它還具有许多其他很酷的功能。
- 5月前4 #
只是在上述Swift版本的答案基础上进行,因為我不得不翻译它:
@IBOutlet weak var numberTextField: UITextField! override func viewDidLoad() { addDoneButtonTo(numberTextField) } // MARK: Done for numberTextField private func addDoneButtonTo(textField: UITextField) { let flexBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "didTapDone:") let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() keyboardToolbar.items = [flexBarButton, doneBarButton] textField.inputAccessoryView = keyboardToolbar } func didTapDone(sender: AnyObject?) { numberTextField.endEditing(true) }
- 5月前5 #
您可以使用
myTextField.inputAccessoryView =_inputView;
input附件视圖是始终位於鍵盤上方並隨
[textfield resignFirstResponder]
一起關闭的视圖在輸入视圖上放入完成並執行文字欄位的signsignfirst响應器。
相似問題
- objective c:確定iPhone是否以程式設計方式被越狱iphoneobjectivecioscocoatouchjailbreak2021-01-10 05:58
- iphone:如何自定義iOS警報视圖?iphoneobjectivecioscocoatouch2021-01-09 21:28
- iphone:如何在iOS的MKAnnotation中添加更多详细資訊iphoneobjectiveciosmkannotation2021-01-12 01:29
- iphone:用小數點輸入數字值的最佳方法是什麼?iphonecocoatouch2021-01-09 17:30
- ios:从UIwebView删除渐變背景?iosiphoneuiwebview2021-01-09 18:24
在iOS7 num-keypad中投射完成按钮的簡單方法.在下面的UITextField委託方法中,為鍵盤顯示添加一个通知。
現在實現方法
keyboardWillShow
如下.在這裏,我们需要特別註意iOS7。現在將此巨集添加到適当的標頭中以檢測SySTEM_VERSION
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)