在某些情况下,我想在iPhone键盘的顶部添加一个工具栏(例如,当您浏览表单元素时,就像在iPhone Safari中一样)。

目前我正在使用常量指定工具栏的矩形,但是由于界面的其他元素不断变化 - 工具栏和屏幕顶部的导航栏 - 每次我们进行次要界面更改时,工具栏都会失去对齐。

有没有办法以编程方式确定键盘相对于当前视图的位置?

有帮助吗?

解决方案

从iOS 3.2开始,有一种新方法可以实现这种效果:

UITextFields UITextViews 具有 inputAccessoryView 属性,您可以将其设置为任何视图,该视图会自动显示在上方并使用键盘设置动画

请注意,您使用的视图既不应该在其他地方的视图层次结构中,也不应该将其添加到某个超级视图中,这是为您完成的。

其他提示

基本上是这样的:

在init方法中:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];

然后使用上面提到的方法来调整条形的位置:

-(void) keyboardWillShow:(NSNotification *) note
{
    CGRect r  = bar.frame, t;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
    r.origin.y -=  t.size.height;
    bar.frame = r;
}

通过将位置更改设置为

,可以使位置更改变得漂亮
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
//...
    [UIView commitAnimations];

这是基于来自tonklon的现有答案 - 我只是添加了一个代码片段,它在键盘顶部显示了一个半透明的黑色工具栏,同时还有一个“完成”的工具栏。右边的按钮:

UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];

UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];

NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];

[flexButton release];
[doneButton release];
[toolbar setItems:itemsArray];

[aTextField setInputAccessoryView:toolbar];

-resignKeyboard 如下所示:

-(void)resignKeyboard {
  [aTextField resignFirstResponder];
}

希望能有所帮助。

如果您注册键盘通知,即 UIKeyboardWillShowNotification UIKeyboardWillHideNotification 等,您收到的通知将包含 userInfo dict( UIKeyboardBoundsUserInfoKey )。

请参阅 UIWindow 类引用。

在3.0及以上版本中,您可以从通知的 userInfo 字典中获取动画持续时间和曲线。

例如,要设置视图大小的动画以便为键盘腾出空间,请注册 UIKeyboardWillShowNotification 并执行以下操作:

- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;

    [UIView commitAnimations];
}

UIKeyboardWillHideNotification 做一个类似的动画。

我发现这个链接非常有用,可以逐步了解inputaccesoryview。

输入附件视图

创建此方法并在ViewWillLoad上调用它:

        - (void) keyboardToolbarSetup
{
    if(self.keyboardToolbar==nil)
        {
        self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];

        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(anyAction)];

        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(anyOtherAction)];


        NSArray *toolbarButtons = [[NSArray alloc]initWithObjects:cancelButton,extraSpace,doneButton, nil];

        [self.keyboardToolbar setItems:toolbarButtons];

        self.myTextView.inputAccessoryView=self.keyboardToolbar;
        }
}

无法获得键盘视图的尺寸(AFAIK)。然而,至少在目前为止每个iPhone版本都是如此。

如果您将工具栏位置计算为距离视图BOTTOM的偏移量,并考虑视图的大小,那么您不必担心是否存在导航栏。

E.g。

#define KEYBOARD_HEIGHT 240 // example - can't remember the exact size
#define TOOLBAR_HEIGHT 30

toolBarRect.origin.y = viewRect.size.height - KEYBOARD_HEIGHT - TOOLBAR_HEIGHT;

// move toolbar either directly or with an animation

您可以轻松创建 keyboardHeight 函数,而不是定义,该函数根据是否显示键盘返回大小,并将此工具栏定位移动到重新组织布局的单独函数中。 / p>

此外,它还取决于您执行此定位的位置,因为根据导航栏设置,视图的大小可能会在加载和显示之间发生变化。我认为最好的地方是viewWillAppear。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top