对于常数UIKeyboardFrameEndUserInfoKey,在Apple文档中它说:
这些坐标不考虑由于界面方向改变而应用于窗口内容的任何旋转因子.因此,您可能需要在使用之前将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法).
所以,如果我使用 [view1 convertRect:rect fromView:view2]
我将为上述参数插入什么以使其正确转换旋转值?即:
view1 =?rect =?(我假设键盘架)view2 =?
一直在尝试一些事情并得到一些有趣的东西.
我正在尝试在键盘出现时滚动文本视图.我试图通过https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html关注Apple自己的教程.我使用autolayout而不是滚动视图和框架,但总体思路是相同的(订阅键盘通知,并相应地设置布局约束).但是,当我显示键盘时,文本视图和键盘之间存在间隙:(这是来自土耳其语键盘的屏幕截图,但是所有安装的键盘中都存在标有红色的相同间隙,包括自定义键盘).(iPhone 6 Plus的屏幕截图,但问题也出现在其他屏幕尺寸上)

我已经看到在iOS8中无法获得键盘高度的正确值,我尝试过使用两者UIKeyboardFrameEndUserInfoKey,UIKeyboardFrameBeginUserInfoKey但它们都会产生相同的差距.我试过安装UIKeyboardWillShowNotification和UIKeyboardDidShowNotification,但仍,我得到了同样的差距.我认为差距与iOS 8上某些键盘上的建议有关,但是当建议不存在时,它不应该报告键盘大小和建议.
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGRect kb = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
float height = kb.size.height;
sendZoneBottomConstraint.constant = height;
[UIView …Run Code Online (Sandbox Code Playgroud) 当显示键盘时,我有一个需要调整位置的UI.
以下用于检测键盘何时显示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
注意:我尝试过UIKeyboardWillShowNotification和UIKeyboardDidShowNotification
- (void)keyboardWillShow:(NSNotification *)n {
if (isKeyboardShowing)
return;
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Animate keyboard shift
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.2f animations:^{
// some animation here
} completion:^(BOOL finished) {
if (finished)
isKeyboardShowing = YES;
}];
Run Code Online (Sandbox Code Playgroud)
对于自定义键盘,键盘大小返回{320,0}.由于键盘现在可以具有不同的高度,因此在呈现键盘时我无法使用静态值来更改UI.
这是ios8的问题吗?有没有其他方法动态获取键盘高度?
编辑:这是userInfo Dict:
{name = UIKeyboardDidShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 0}}"; …Run Code Online (Sandbox Code Playgroud) 知道如何让 inputAccessoryView 锚定到选项卡栏而不是屏幕底部吗?
我创建了一个 UIViewController 并覆盖了以下方法:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(UIView *)inputAccessoryView {
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
self.keyboardInputAccessoryView =[[BRKeyboardInputBarView alloc] initWithFrame:frame leftButtonTitle:@"Left" andRightButtonTitle:@"Send"];
[self.keyboardInputAccessoryView setDelegate:self];
[self.keyboardInputAccessoryView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.keyboardInputAccessoryView removeFromSuperview];
return self.keyboardInputAccessoryView;
}
Run Code Online (Sandbox Code Playgroud)
带有 inputAccessoryView 的视图控制器覆盖标签栏
从它的外观来看,视图控制器将视图添加到窗口而不是当前视图控制器视图,这将解释其定位。但是,如果我删除该行:
[self.keyboardInputAccessoryView removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
当我点击附件视图的文本视图时发生崩溃:
The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>
Run Code Online (Sandbox Code Playgroud)
所以我想我要问的是添加键盘附件视图的正确方法是什么,以便它可以很好地与自动布局配合使用并避免崩溃,而且还可以将自身锚定到视图而不是窗口?
我有一些实例UITextView,我希望它textView在显示键盘时占据所有空白的垂直空间。问题是我不知道键盘的高度是多少,因为它在 iOS 8 中具有预测栏,并且当键盘已经显示时用户可以更改其实际高度。我不想改变textView的autocorrectionType。我对那个酒吧很好,只是想以正确的方式处理它。
所以问题是:是否有可能知道这个栏是否可见?是否有任何方法可以触发用户滑动来显示/隐藏此栏?
提前致谢。
我在iOS 9,XCode 7 GM上运行.我有一个扩展UITextField(CustomOffsetTextField)的类,并提供对自定义文本定位的支持,如下所示:
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}
Run Code Online (Sandbox Code Playgroud)
leftViewOffset是文本字段leftView的宽度(如果存在).textOffset是一个CGPoint,它定义要应用于文本rects的自定义x和y偏移量.
我的问题出现在我的签到视图中.我有2个CustomOffsetTextField实例 - 一个用于用户的电子邮件,另一个用于他们的密码.
在第一次加载视图控制器时,如果我在一个字段中输入文本然后点击另一个字段,那么该文本将在跳回到textRectForBounds定义的位置之前短暂地跳回其文本字段中的位置0,0.一些基本的打印调试验证这些函数总是返回我期望它们的值.
在这个初始打嗝之后,文本字段的行为就像我期望的那样.此问题仅在视图控制器加载后在每个文本字段中出现一次.在那之后,我可以在我想要的地方之间来回切换,而不会再次发生.
有没有人在iOS 9中看到与UITextField类似的问题?如果是这样,你能找到修复方法吗?
使用自定义键盘时,keyboardWillShow运行两次(正常行为),第一个高度为0,但第二个在我的情况下为正确的高度667。问题是,这仅在第二次显示viewController时才是正确的。我第一次得到下面的奇怪输出。
第一次打开视图控制器时进行控制台:
keyboardSize CGRect(来源=(x = 0,y = 258),大小=(宽度= 0,高度= 2.8876618518302306E-314))
第二次打开视图控制器时进行控制台:
keyboardSize CGRect(来源=(x = 0,y = 0),大小=(宽度= 0,高度= 667))
我的代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if keyboardSize.height > 0 { //in case of custom keyborad
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码来检测键盘何时显示并获取其高度。
@objc func keyboardWasShown (_ notification: Notification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size;
print ("KEYBOARD SIZE: \(keyboardSize.height)");
}
Run Code Online (Sandbox Code Playgroud)
起初,当我第一次点击 UITextView 使其成为第一响应者时,控制台显示 271.0 点。
然后我点击视图以关闭键盘。然后我再次点击 UITextView。现在它显示 226.0 点。
第一次和后续试用中的键盘布局没有变化。一开始我总是得到 271,然后下一个总是 226。正确的是 271。
为什么会这样?以及如何修复它?
ios ×7
uikeyboard ×3
cocoa-touch ×2
ios8 ×2
objective-c ×2
swift ×2
ios9 ×1
iphone ×1
keyboard ×1
uikit ×1
uitextfield ×1
uitextview ×1
uiview ×1
xcode6 ×1