【问题标题】:Get size of keyboard keys programmatically in iOS with Swift or Objective-C使用 Swift 或 Objective-C 在 iOS 中以编程方式获取键盘键的大小
【发布时间】:2015-06-04 17:07:59
【问题描述】:

我知道我可以使用 userInfo 字典中的 keyboardWasShown 回调来获取键盘本身的大小。但是我需要知道的是所述键盘上实际按键的大小。
我想使用这些信息来复制键盘上方的额外按键。 我不想制作自定义键盘,因为我不希望在此应用程序之外使用此键盘。我只想要上面的几个额外键作为某些键的快捷方式。
有没有办法找到键的大小(iPhone 和 iPad 应该不同)。

【问题讨论】:

  • 你能找到任何解决方案吗?

标签: ios objective-c swift keyboard


【解决方案1】:

在 ViewWillAppear 方法中添加观察者

 - (void)viewWillAppear:(BOOL)animated
   {

  [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
}
-(void) keyboardWillShow:(NSNotification *)note{
 CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

 // Need to translate the bounds to account for rotation.

keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
CGFloat keyboardHeight= keyboardBounds.size.height;
}

- (void)viewWillDisappear:(BOOL)animated
 {
    [super viewWillDisappear:animated];
   // unregister for keyboard notifications while not visible.
       [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillShowNotification
                                              object:nil];


 }

【讨论】:

  • 这是获取键盘高度还是键盘按键高度?我正在寻找键盘键的高度。
  • 对不起约翰斯顿,这将只对键盘高度有帮助。如果您发现任何用于获取键盘按键大小的信息,请发布。
  • 感谢您的帮助,但是:您是否阅读了帖子的第一句话? "我知道我可以使用keyboardWasShown的回调来获取键盘本身的大小"
猜你喜欢
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 2017-11-19
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
相关资源
最近更新 更多