簡體   English   中英

使用Swift或Objective-C在iOS中以編程方式獲取鍵盤按鍵的大小

[英]Get size of keyboard keys programmatically in iOS with Swift or Objective-C

我知道我可以使用userInfo字典中keyboardWasShown的回調函數來獲取鍵盤本身的大小。 但是我需要知道的是所述鍵盤上實際按鍵的大小。
我想使用此信息在鍵盤上方復制多余的鍵。 我不想制作自定義鍵盤,因為我不想在此應用程序之外使用此鍵盤。 我只希望上面幾個額外的鍵成為某些鍵的快捷方式。
有沒有一種方法可以找到按鍵的大小(iPhone和iPad的大小應該不同)。

在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];


 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM