我曾经使用以下代码在UIKeyboard上方添加一个UIToolbar并附加到它上面。我刚刚切换到iPhone OS 4,我意识到它已不再适用了。
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
//print all uiview descriptions
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
.. Some code to attach the UIToolbar
}
}
}
我意识到代码没有进入if语句。我尝试在if上方打印出所有UIView描述,但是我看不到任何与UIKeyboard相关的内容。该代码在OS 3.0和3.1中运行良好。那么有人对此有任何想法吗?
答案 0 :(得分:1)
您应该尝试使用以下代码:
- (BOOL) findKeyboard:(UIView *) superView;
{
UIView *currentView;
if ([superView.subviews count] > 0) {
for(int i = 0; i < [superView.subviews count]; i++)
{
currentView = [superView.subviews objectAtIndex:i];
NSLog(@"%@",[currentView description]);
if([[currentView description] hasPrefix:@"<UIKeyboard"] == YES)
{
NSLog(@"Find it");
//add toolbar here
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -40, 100, 40)];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test button" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonClicked:)];
NSArray *items = [[NSArray alloc] initWithObjects:barButtonItem, nil];
[toolbar setItems:items];
[items release];
[currentView addSubview:toolbar];
return YES;
}
if ([self findKeyboard:currentView]) return YES;
}
}
return NO;
}
-(void) checkKeyBoard {
UIWindow* tempWindow;
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
if ([self findKeyboard:tempWindow])
NSLog(@"Finally, I found it");
}
}
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:(@selector(checkKeyBoard)) withObject:nil afterDelay:0];
}
您还需要将此代码添加到AppDelegate中的didFinishLaunchingWithOptions函数中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
答案 1 :(得分:0)
由于您依赖于未记录的行为,因此它不再适用于4.0。请改用视图'inputAccessoryView
属性,这是在OS 3.2 +中执行此操作的文档化方法。