如何在iPhone的虚拟键盘(触摸UITextField时出现的键盘)中添加新按钮?
最佳答案
这是我在键盘左下角添加完成按钮的代码。
您需要做的就是借鉴 thiis 的想法,并将按钮设置在键盘上您想要的任何位置。
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
//[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
//[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
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)
{
if(isNumPad==YES)
{
//[keyboard addSubview:doneButton];
}
else
{
[doneButton removeFromSuperview];
}
}
else
{
[doneButton removeFromSuperview];
}}
}
- (void)doneButton:(id)sender
{
//YOUR CODE ON DONE BUTTON CLICKED
}
快乐编码...
关于iphone - iPhone虚拟键盘添加新按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3956648/