ios – 当键盘处于活动状态时呈现模态视图控制器

所以我基本上有一个表单,由几个文本字段组成.用户像往常一样键入字段.但用户还可以选择双击文本字段,该文本字段提供了模态视图控制器,允许用户从与该字段相关的多个选项中进行选择.

我可以以某种方式呈现模式“over”键盘,这样当它被关闭时,键盘仍然是活动的领域的第一反应者之前,我提出了模态?

现在,模态出现时,键盘解除关闭,并随着模式被关闭而重新出现.它对我来说看起来很笨重,分心.喜欢简化它,并减少屏幕上的动画数量.

解决方法

您可以创建一个新的UIWindow并将其放在认窗口上.使用这种技术,键盘仍然在那里,但它隐藏在新窗口后面.

我在Github here上有一个示例项目,但基本过程如下.

>为您的模态视图创建一个新的UIViewController类.我打电话给我的OverlayViewController.根据需要设置相应的视图.根据您的问题,您需要传回一些选项,所以我做了一个委托协议OverlayViewController,并将使主窗口的根视图控制器(ViewController类)成为我们的委托.

@protocol OverlayViewControllerDelegate <NSObject>
@required
- (void)optionChosen:(int)option;
@end

@interface OverlayViewController : UIViewController
@property (nonatomic,weak) id<OverlayViewControllerDelegate> delegate;
@end

>为我们的原始视图控制器添加一些支持属性.

@interface ViewController () <UIGestureRecognizerDelegate,UITextFieldDelegate,OverlayViewControllerDelegate>
// The text field that responds to a double-tap.
@property (weak) IBOutlet UITextField *textField;
// A simple label that shows we received a message back from the overlay.
@property (weak) IBOutlet UILabel *label;
// Gesture recognizer for the text field
@property (strong) UITapGestureRecognizer *doubleTapRecognizer;
// The window that will appear over our existing one.
@property (strong) UIWindow *overlayWindow;
@end

>将UITapGestureRecognizer添加到您的UITextField.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Perform any setup you need here
    // ...

    self.doubleTapRecognizer = [[UITapGestureRecognizer alloc]
                                 initWithTarget:self
                                 action:@selector(handleDoubleTap)];
    self.doubleTapRecognizer.numberOfTapsrequired = 2;
    self.doubleTapRecognizer.delegate = self;
    [self.textField addGestureRecognizer:self.doubleTapRecognizer];
}

> UITextField具有内置的手势识别器,因此我们需要允许多个UIGestureRecognizer同时操作.

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

>这是有趣的部分.触发手势识别器时,创建新的UIWindow,将您的OverlayViewController分配为根视图控制器,并显示它.请注意,我们将窗口级别设置为UIWindowLevelAlert.你不能在这里使用UIWindowLevelnormal,因为键盘仍然在前面.

- (void)handleDoubleTap {
    self.overlayWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
    [self.overlayWindow setwindowLevel:UIWindowLevelAlert];
    OverlayViewController *overlayVC = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    self.overlayWindow.rootViewController = overlayVC;
    overlayVC.delegate = self;
    [self.overlayWindow makeKeyAndVisible];
}

>覆盖窗口现在覆盖原始窗口和键盘.用户现在可以选择您在叠加层视图中构建的任何选项.在您的用户选择一个选项后,您的委托应该采取您打算的任何操作,然后关闭覆盖窗口.

- (void)optionChosen:(int)option {
    // Your code goes here. Take action based on the option chosen.
    // ...

    // dismiss the overlay
    self.overlayWindow = nil;
}

>覆盖窗口应该消失,原来的窗口应该与键盘的位置相同.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...