首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式将工具栏与iPhone键盘顶部对齐

以编程方式将工具栏与iPhone键盘顶部对齐
EN

Stack Overflow用户
提问于 2008-10-01 16:43:34
回答 7查看 68.8K关注 0票数 94

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

目前我正在用常量指定工具栏的矩形,但是因为界面的其他元素都在通量中--屏幕顶部的工具栏和导航栏--每次我们对界面做一个小的改变,工具栏就会失去对齐。

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

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-07-24 02:15:25

从iOS 3.2开始,有了一种新的方法来实现这种效果:

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

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

票数 141
EN

Stack Overflow用户

发布于 2008-12-28 05:37:29

所以基本上:

在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];
票数 72
EN

Stack Overflow用户

发布于 2011-05-17 03:52:33

这是基于existing answer from 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];
}

希望这能帮助到一些人。

票数 60
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/158574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档