تعديل واجهة لوحة المفاتيح عندما يظهر UITextField أو UITextView

StackOverflow https://stackoverflow.com/questions/402658

  •  03-07-2019
  •  | 
  •  

سؤال

لدي جدول به كل خلية تحتوي على تسمية حقل "نص".المشكلة هي أنه عندما أذهب إلى تحرير الصف الأخير, لوحة المفاتيح يخفي الجزء السفلي من الجدول ، وأنا لا يمكن أن نرى ما يتم كتابتها.كيف يمكنني نقل واجهة فوق لوحة المفاتيح حتى أرى ما يجري كتبته?

شكرا مصطفى

هل كانت مفيدة؟

المحلول

وأنت تريد التسجيل viewController عن UIKeyboardDidShowNotification وUIKeyboardWillHideNotification الأحداث. عندما تحصل هذه، يجب ضبط حدود الجدول الخاص بك. لوحة المفاتيح هو ارتفاع 170 بكسل، وذلك فقط تقليص أو زيادة حدود الجدول بشكل مناسب، ويجب ضبط بشكل صحيح على لوحة المفاتيح.

نصائح أخرى

هذه المشكلة معقدة اعتمادا على واجهة المستخدم الخاصة بك السيناريو.هنا سوف نناقش سيناريو حيث UITextField أو UITextview يقيم في UITableViewCell.

  1. تحتاج إلى استخدام NSNotificationCenter للكشف عن UIKeyboardDidShowNotification الحدث.انظر http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html.تحتاج إلى تقليص UITableView حجم الإطار بحيث تحتل سوى مساحة الشاشة التي لا تغطي لوحة المفاتيح.

    1. إذا قمت بالنقر فوق UITableViewCell ، OS سيتم تلقائيا وضع الخلية في منطقة المشاهدة من UITableView.ولكن ذلك لا يحدث عند النقر على UITextView أو UITableViewCell على الرغم من أنه يقيم في UITableViewCell.

تحتاج إلى استدعاء

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

برمجيا "الحنفية" الخلية.

إذا كنت تنفيذ كل النقاط, سوف ترى UITextView/موقف ميدان الحق فوق لوحة المفاتيح.عارية في الاعتبار أن UITableViewCell حيث UITableView/حقل موجود لا يمكن أن يكون أطول من "لا تغطي" المنطقة.إذا كان هذا ليس هو الحال بالنسبة لك ، هناك نهجا مختلفا عن ذلك ولكن أنا لن أناقش هنا.

وفقط للتأكد من أن لديك مساحة كافية التمرير دون ذلك. لأنه وفقا لمعرفتي فون يضبط تلقائيا ويظهر النص تركيزا في الوقت يبدو وحة المفاتيح لها.

وإزالة / معرض تعليقه على خطوط حيث يبدو أن هايت احداثيات يتم تعديلها من أجل حل المشكلة. شكرا.

ورمز التعديل:

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

و}

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardVisible )
        return;

    if( activeTextField != MoneyCollected)
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) 
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

وهذه محاولة بلدي الترميز هذا سيساعد لأجلك ..

scroll top