ios3.2/4查找键盘窗口的方式改变了

本文介绍了一种在iOS应用中监听键盘显示并添加自定义隐藏按钮的方法。通过观察UIKeyboardWillShowNotification通知,文章详细展示了如何找到键盘视图,并在其上添加一个用于隐藏键盘的按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://gist.github.com/454844

实际上应该是键盘窗口的类发生了变化,由UIKeyboard变成了UIPeripheralHostView。

 

- (void)someSetupMethod {
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillShow:)
                                               name:UIKeyboardWillShowNotification object:nil];
}

- (void)someTeardownMethod {
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}


- (void)keyboardWillShow {
  // We must perform on delay (schedules on next run loop pass) for the keyboard subviews to be present.
  [self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0]; 
}


- (void)addHideKeyboardButtonToKeyboard {
  
  // Locate non-UIWindow.
  UIWindow *keyboardWindow = nil;
  for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
      keyboardWindow = testWindow;
      break;
    }
  }
  if (!keyboardWindow) return;

  // Locate UIKeyboard.  
  UIView *foundKeyboard = nil;
  for (UIView *possibleKeyboard in [keyboardWindow subviews]) {

    // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
    if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
      possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
    }                                                                                
    
    if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
      foundKeyboard = possibleKeyboard;
      break;
    }
  }
  
  if (foundKeyboard) {
    // Add the button to foundKeyboard.
  }
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值