don*_*ald 7 uitextfield autoscroll ios uitextfielddelegate
我有一个应用程序,我必须在键盘显示的情况下向上滚动.为了获得键盘大小,我正在注册UIKeyboardWillShowNotification事件,如下所示:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window]
Run Code Online (Sandbox Code Playgroud)
这确实有效,问题是,在调用textFieldDidBeginEditing之后调用它.因此,我无法获得实际的键盘大小,但只有在该字段已经处于编辑模式之后,这才会在首先注册此事件的整个目的.我确定我已经调用了UIKeyboardWillShowNotification而不是UIKeyboardDidShowNotification,尽管切换这两个产生了相同的结果:首先调用委托方法,然后调用通知方法.关于如何扭转局面的任何想法?目前我很难编码大小,这是非常糟糕的做法......
我在 XCode 5 中打开了一个新项目,添加了一个UITextField并ViewController连接了它的委托。
这是我唯一的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationMethod:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)myNotificationMethod:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
NSLog(@"Rect: %@",NSStringFromCGRect(keyboardFrameBeginRect));
}
Run Code Online (Sandbox Code Playgroud)
这是日志输出:
肖像:
Rect: {{0, 480}, {320, 216}}
Run Code Online (Sandbox Code Playgroud)
景观:
Rect: {{-162, 0}, {162, 480}}
Run Code Online (Sandbox Code Playgroud)
编辑:
关于textFieldDidBeginEditingbefore 被调用name:UIKeyboardWillShowNotification,我真的不明白为什么 textField 是否处于编辑模式会有差异,但有几种方法可以解决这个问题。
从 textFieldShouldBeginEditing 保存对 textField 的引用,并在 myNotificationMethod 中使用它(如果触发了 textFieldShouldBeginEditing)。
像这样玩UIResponder:
在textFieldDidBeginEditing-> 保存对 的引用UIResponder并将 更改UIResponder为与临时无关的引用。在myNotificationMethod对 textField 执行您想要执行的操作(即不在编辑模式\第一响应者)中,完成后将其设为 main UIResponder。
| 归档时间: |
|
| 查看次数: |
2580 次 |
| 最近记录: |