ios 以编程方式对齐 iPhone 键盘顶部的工具栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:17:58  来源:igfitidea点击:

Programmatically align a toolbar on top of the iPhone keyboard

iosiphonecocoa-touchkeyboard

提问by Rob Drimmie

In several cases I want to add a toolbar to the top of the iPhone keyboard (as in iPhone Safari when you're navigating form elements, for example).

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

Currently I am specifying the toolbar's rectangle with constants but because other elements of the interface are in flux - toolbars and nav bars at the top of the screen - every time we make a minor interface change, the toolbar goes out of alignment.

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

Is there a way to programmatically determine the position of the keyboard in relation to the current view?

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

回答by tonklon

As of iOS 3.2 there's a new way to achieve this effect:

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

UITextFieldsand UITextViewshave an inputAccessoryViewproperty, which you can set to any view, that is automatically displayed above and animated with the keyboard.

UITextFieldsUITextViews有一个inputAccessoryView属性,您可以将其设置为任何视图,该属性会自动显示在上方并使用键盘进行动画处理。

Note that the view you use should neither be in the view hierarchy elsewhere, nor should you add it to some superview, this is done for you.

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

回答by Rob Drimmie

So basically:

所以基本上:

In the init method:

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

And then have methods referred to above to adjust the position of the bar:

然后有上面提到的方法来调整酒吧的位置:

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

Could make it pretty by animating the position change by wrapping it in

可以通过将其包裹在动画位置变化来使它漂亮

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
//...
    [UIView commitAnimations];

回答by phi

This is based on the existing answer from tonklon- I'm just adding a code snippet that shows a semi transparent black toolbar on top of the keyboard, together with a "done" button on the right:

这是基于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];

and the -resignKeyboardlooks like:

-resignKeyboard看起来像:

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

Hope that helps someone.

希望能帮助某人。

回答by amrox

If you register for keyboard notifications, ie UIKeyboardWillShowNotificationUIKeyboardWillHideNotification, etc, the notification you receive will contain the bounds of the keyboard in the userInfodict (UIKeyboardBoundsUserInfoKey).

如果您注册了键盘通知,即UIKeyboardWillShowNotificationUIKeyboardWillHideNotification,等,您收到的通知将在userInfodict ( UIKeyboardBoundsUserInfoKey) 中包含键盘的边界。

See the UIWindowclass reference.

请参阅UIWindow类参考。

回答by David Beck

In 3.0 and above you can get the animation duration and curve from the userInfodictionary of the notifications.

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

for instance, to animate the size of the view to make room for the keyboard, register for the UIKeyboardWillShowNotificationand do something like the following:

例如,要为视图的大小设置动画以为键盘腾出空间,请注册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];
}

Do a similar animation for UIKeyboardWillHideNotification.

为 做一个类似的动画UIKeyboardWillHideNotification

回答by Juan Sebastián López

Create this method and call it on ViewWillLoad:

创建此方法并在 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;
        }
}

回答by Andrew Grant

There's no way (AFAIK) to get the dimensions of the keyboard view. It is however constant, at least in every iPhone version so far.

没有办法(AFAIK)获得键盘视图的尺寸。然而,它是恒定的,至少在迄今为止的每个 iPhone 版本中都是如此。

If you calculate the toolbar position as an offset from the BOTTOM of your view, and take the size of your view into account, then you should not have to worry whether a navbar is present or not.

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

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

Instead of a define, you could easily create a keyboardHeightfunction that returns the size based on whether the keyboard is being displayed, and move this toolbar positioning into a separate function that reorganizes your layout.

代替定义,您可以轻松创建一个keyboardHeight函数,该函数根据键盘是否正在显示来返回大小,并将此工具栏定位移动到一个单独的函数中,以重新组织您的布局。

Also it can depend on where you do this positioning as it's possible the size of your view may change between being loaded and shown based on your navbar setup. I believe the best place to do it would be in viewWillAppear.

此外,它可以依靠,因为它是可以被加载并根据您的导航栏设置显示您的视图之间可能会改变大小,你做这个定位。我相信这样做会在viewWillAppear中的最佳场所。