How to add done button on keyboard on top of keyboard in IOS?

前端 未结 8 1569
悲哀的现实
悲哀的现实 2020-12-08 04:04

I want to add the Done button on the top of keyboard on right side in iOS for a textView.Please tell me how can i do that?

I w

8条回答
  •  粉色の甜心
    2020-12-08 04:41

    Hope this help :)

    UIToolbar* keyboardToolbar = [[UIToolbar alloc] init];
    [keyboardToolbar sizeToFit];
    UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                      target:nil action:nil];
    UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                      target:self action:@selector(yourTextViewDoneButtonPressed)];
    keyboardToolbar.items = @[flexBarButton, doneBarButton];
    self.yourTextView.inputAccessoryView = keyboardToolbar;
    

    and then add yourTextViewDoneButtonPressed method

    -(void)yourTextViewDoneButtonPressed
    {
        [self.yourTextView resignFirstResponder];
    }
    

提交回复
热议问题