iOS5中如何显示被键盘隐藏的UIElements?

发布于 2024-12-11 13:03:39 字数 213 浏览 0 评论 0原文

在 iOS 5 中,iPad 支持 3 种不同的键盘(普通、分离、上移)。以前,当键盘出现时,控制器将通过 KeyboardDidShowNotification 收到通知。在这里,如果我们有任何被键盘隐藏的 UI 元素,则会设置一个偏移量并将元素向上推(通过使用滚动视图)。在 iOS 5 中,我们必须根据键盘的类型进行处理。我们如何知道键盘类型。我们可以为新的键盘类型做什么?

谢谢, 杜赖。

In iOS 5 iPad supports 3 different keypads(normal, split, move-up). Previously when keypad appears controller will be notified though KeyboardDidShowNotification. Here, if we have any UI elemtns hidden by keypad will set an offset and push the elements upwards(by using scroll view). In iOS 5 we have to handle based on the type of the keypad. How do we know about the keypad type. What we can do for the new keypad-types?.

Thanks,
durai.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

已下线请稍等 2024-12-18 13:03:39

如果您想在单击编辑文本元素时自动滚动键盘下方隐藏的 textView 或 textField 元素,以下代码将在 ios5 上帮助您:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:_activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _activeField = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    [self registerForKeyboardNotifications];
}

If you want to scroll automatically hidden textView or textField elements beneath keypad when click on edit text element, following code will help you for ios5:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:_activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _activeField = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    [self registerForKeyboardNotifications];
}
孤独陪着我 2024-12-18 13:03:39

如果您对 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 做出反应,您应该没问题,因为它们仅在键盘以“正常模式”显示时发送。如果用户将其拉起或分开它,你会收到一个 UIKeyboardWillHideNotification (奇怪的行为,但苹果唯一的选择,使其向后兼容 iOS 4 应用程序)

If you react on UIKeyboardWillShowNotification or UIKeyboardWillHideNotification you should be fine as they are only sent when the keyboard is shown in "normal mode".. if the user pulls it up or splits it, you will recieve a UIKeyboardWillHideNotification (strange behaviour but apple's only choice to make it backwards compatible with iOS 4 apps)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文