2011-05-18 105 views
11

我使用“setInputView”函数使用自定义键盘实现了文本字段。 但我有一个问题:我的键盘框架不是标准的iphone keybord框架。自定义键盘:inputView:如何更改键盘大小?

现在的问题是: 如何更改我的自定义键盘的大小? 我知道一些功能,如:UIKeyboardFrameBeginUserInfoKey,..等等。

请注意: iPhone的键盘边框为= 0264320216 我的自定义键盘边框为= 0,0,320,460

希望能为你的那种合作, 最好的问候...... P

回答

2

我有同样的问题。我通过注册UIKeyboardDidShowNotification(不幸的是,UIKeyboardWillShowNotification不工作),然后在显示键盘后更改视图大小来解决此问题。但是,当它向上移动时,它仍然在键盘上方有一个白色的盒子。这对我来说很好,因为它是通过具有白色背景的UITextView进入的。但是,如果你进入其他有色物体,在视图正确调整大小之前,它会显得有点难看。您可以通过将背景色设置为clearColor来解决此问题。

// Add this while initializing your view 
self.backgroundColor = [UIColor clearColor]; // Needed because we can't resize BEFORE showing the view. Otherwise you will see an ugly white box moving up w/ the keyboard 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWasShown:) 
              name:UIKeyboardDidShowNotification object:nil]; 



// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 

    CGRect rect = self.frame; 
    rect.size.height = 164; 
    self.frame = rect; 

} 
27

事实证明,您分配到UITextField的属性自定义输入视图的默认行为是调整视图大小相同的帧作为默认键盘。尝试设置(我用的名字InputViewController我输入视图,但你可以使用任何你想要的):

inputViewController = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:nil]; 
inputViewController.delegate = self; 
inputViewController.view.autoresizingMask = UIViewAutoresizingNone; // This is the code that will make sure the view does not get resized to the keyboards frame. 

有关详细信息,你可以看看this link,这是由苹果提供:

如果UIKit在其自动识别掩码中遇到具有UIViewAutoresizingFlexibleHeight值的输入视图,它将更改高度以匹配键盘。

希望有帮助!

+0

Gr8帮助。 Thax。 – 2012-08-13 14:41:33

+0

这样做。 – Wollan 2012-09-30 10:45:43

+0

@msgambel我已经为我的inputView设置了autoresizingMask到UIViewAutoresizingNone,但它仍然被调整到键盘的框架。你能帮我解决这个问题吗?http://stackoverflow.com/questions/13394564/ios-customise-uitextfields-inputview-size – applefreak 2012-11-15 11:45:56

0

对我而言msgambel的解决方案无法正常工作。但方法正确,我正在使用inputView的autoresizingMask。前我有不同的设置,但要避免在定制键盘白色额外空间的正确方法是:

enter image description here

我申请这只是最外面的景色。

enter image description here

9

要具有相同大小的原生键盘设置键盘inputView只是这样做:

inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

要设置自己的框架做到这一点:

inputView.autoresizingMask = UIViewAutoresizingNone; 

From Apple

您在定义输入视图或输入附件视图的大小和内容方面有很大的灵活性。尽管这些视图的高度可以是您想要的,但它们应该与系统键盘的宽度相同。如果UIKit在其自动识别掩码中遇到具有UIViewAutoresizingFlexibleHeight值的输入视图,则会更改高度以匹配键盘。输入视图和输入附件视图可能具有的子视图(如控件)的数量没有限制。有关输入视图和输入附件视图的更多指导,请参阅iOS人机界面指南。

+1

,我们赢了! – 2015-07-29 11:55:45

+0

对我来说,这是行不通的。我没有设置inputView.autoresizingMask = [];并仍然显示默认的键盘高度。 – Ramis 2016-10-27 07:13:09

+0

@Ramis将它设置为none – 2016-10-27 08:49:19

0

此外,如果你正在使用一个UIViewController来设计你的inputView,不使用UIViewController.view ......它似乎有很多问题上得到正确旋转调整不论AutoresizeMask的。

对我来说有效的是拿走我现有的UI并使用Editor> Embed In> View。然后创建一个新的出口,并将该出口作为inputView传递。突然间,旋转bug的大小消失了。