How can I keep the keyboard present even when presenting a modal view controller?

十年热恋 提交于 2019-12-04 02:19:47

问题


I've got a modal view controller that is presented and a UITextView becomes the first responder and the keyboard is shown.

Once this screen is loaded, the user can categorize their input before submitting it. This happens by way of another modal view controller that is presented on top.

When this second one is presented, the keyboard is dismissed, the user chooses, and then reappears when the initial UITextView becomes first responder again.

How can I present the second modal view controller without ever dismissing the keyboard?

EDIT: I've implemented part of the UITextViewDelegate, and I'm still not getting the desired result.

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return NO;
}

回答1:


You can't do this using presentModalViewController:animated:. You'll have to put your modal view in a separate UIWindow, set that second UIWindow's windowLevel to something high (like UIWindowLevelStatusBar), and animate it on and off the screen yourself. You won't need a second view controller at all.

In your XIB, make a new top-level UIWindow object. Put your second view into this window. Connect the window to an outlet on your view controller. (I called the outlet otherWindow in my test code but overlayWindow would be a better name. The outlet needs to be declared strong or retain.)

In your view controller, implement these methods:

- (IBAction)presentOverlay:(id)sender
{
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    frame.origin.y += frame.size.height;
    self.otherWindow.frame = frame;
    self.otherWindow.windowLevel = UIWindowLevelStatusBar;
    self.otherWindow.hidden = NO;
    [UIView animateWithDuration:.25 animations:^{
        self.otherWindow.frame = [UIScreen mainScreen].applicationFrame;
    }];
}

- (IBAction)dismissOverlay:(id)sender
{
    [UIView animateWithDuration:.25 animations:^{
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        frame.origin.y += frame.size.height;
        self.otherWindow.frame = frame;
    } completion:^(BOOL completed){
        self.otherWindow.hidden = YES;
    }];
}

Use these to present and dismiss the overlay view.



来源:https://stackoverflow.com/questions/8161207/how-can-i-keep-the-keyboard-present-even-when-presenting-a-modal-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!