编程方式对齐工具栏上的iPhone键盘上方(Programmatically align a too

2019-08-20 02:26发布

在一些情况下,我想一个工具栏(在iPhone Safari浏览器,当您使用导航表单元素,例如)添加到iPhone键盘的顶部。

目前,我指定工具栏的矩形与常量,但由于接口的其他元素都在不断变化 - 工具栏和导航栏在屏幕的顶部 - 每次我们稍作界面的变化,工具栏超出对齐。

有没有一种方法以编程方式确定键盘在关系到当前视图的位置?

Answer 1:

随着iOS 3.2的有达到这种效果的新方法:

UITextFieldsUITextViewsinputAccessoryView属性,它可以设置为任何观点,那就是上面自动显示,并与键盘动画。

请注意,您应该使用既不是在其他地方的视图层次,也不应该将其添加到一些上海华认为,这是为你做。



Answer 2:

所以基本上:

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


Answer 3:

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

希望可以帮助别人。



Answer 4:

如果您注册键盘的通知,即UIKeyboardWillShowNotification UIKeyboardWillHideNotification等,您会收到通知将包含键盘中的边界userInfo字典( UIKeyboardBoundsUserInfoKey )。

看到UIWindow类的引用。



Answer 5:

在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



Answer 6:

我发现这个链接来了解一步一步inputaccesoryview非常有用的。

输入附属视图



Answer 7:

创建这个方法并调用它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;
        }
}


Answer 8:

有没有办法(据我所知)来获取键盘视图的维度。 然而不变,至少在每一个iPhone版本,到目前为止。

如果计算工具栏位置作为从视图的底部偏移,并把你的视图的大小考虑进去,那么你不应该担心一个导航栏是否存在与否。

#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返回基于是否正在显示的键盘大小函数,并移动此工具栏定位到一个单独的函数,重组布局。

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



文章来源: Programmatically align a toolbar on top of the iPhone keyboard