سؤال

هذا موضوع شائع جدًا ولديه العديد من الإجابات هناك.

الموقف: لدي شاشة ملء (ناقص شريط الأدوات) uitextview مع uitoolbar في الأسفل. عندما يحصل uitextview على المستجيب الأول ، أريد أن أحصل على شريط الأدوات مع لوحة المفاتيح وإضافة زر "تم" الذي سوف يرفض لوحة المفاتيح.

حتى الآن: لدي هذا يعمل تمامًا ، بناءً على هذا المثال. باستثناء حقيقة أنه عندما أضع [textView becomeFirstResponder]; في viewDidLoad ثم شريط الأدوات لا ينشط. بالرغم من keyboardWillShow يسمى. هل يوجد لدى أحد أي فكرة؟

شفرة: فقط حتى لا تضطر إلى التحقق من رمز المثال هذا ما يحدث:

في ViewDidload:

- (void)viewDidLoad {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
   [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   [textView becomeFirstResponder];
       [super viewDidLoad];
}

في لوحة المفاتيح:

- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                            target:self 
                                                                            action:@selector(keyboardDone)];
    NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
    [toolbarItems addObject:doneButton];
    [toolbar setItems:toolbarItems];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;
     [UIView commitAnimations];
 }
هل كانت مفيدة؟

المحلول

حاول تحريك -becomeFirstResponder دعوة ل -viewWillAppear:animated: أو -viewDidAppear:animated:. أظن -viewDidLoad عادة ما يسمى قبل إضافة العرض بالفعل إلى التسلسل الهرمي للعرض.

نصائح أخرى

أضف هذا في الكود الخاص بك

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    UIToolbar* keyboardDoneButtonView   = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle     = UIBarStyleBlack;
    keyboardDoneButtonView.translucent  = YES;
    keyboardDoneButtonView.tintColor    = nil;
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton    = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered  target:self action:@selector(doneBtnTapped:)];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer1    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                                target:nil action:nil];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:nil action:nil];

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:spacer, spacer1, doneButton, nil]];

    // Plug the keyboardDoneButtonView into the text field...
    textView.inputAccessoryView = keyboardDoneButtonView;

    return YES;
}

- (void)doneBtnTapped:(id)sender {
     [yourTextView resignFirstResponder];
}

وكل شيء تم فعله ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top