ios 键盘可点击但不可见

ios keyboard clickable but not visible

我正在制作一个允许用户提问的应用程序。在我的 addQuestion class 中,我有一个 UITextView,用户可以在其中写下他们的问题。

我的问题是,在我的 iPhone 上,键盘偶尔会不可见(这种情况不会每次都发生——只有在我第一次打开应用程序时——有时它甚至可以立即运行)。奇怪的是,如果键盘可见,我仍然可以按下键盘的键并根据键的位置键入响应。问题只是可见性(我尝试移动 UITextView 以使其不会阻塞键盘)。大约 20 或 25 秒后,键盘出现。

我也使用了苹果推荐的排错方式:

for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        NSLog(@"isKeyWindow = %d window level = %.1f frame = %@ class = %@\n",
              window.isKeyWindow, window.windowLevel,
              NSStringFromCGRect(window.frame), window.class.description);
    }

但没有自定义 window 覆盖 UITextEffectsWindow

它打印出:

isKeyWindow = 1 window level = 0.0 frame = {{0, 0}, {375, 667}} class = UIWindow**

isKeyWindow = 0 window level = 10.0 frame = {{0, 0}, {375, 667}} class = UITextEffectsWindow**

当它最终工作并且键盘可见时:

isKeyWindow = 1 window level = 0.0 frame = {{0, 0}, {375, 667}} class = UIWindow

isKeyWindow = 0 window level = 1.0 frame = {{0, 0}, {375, 667}} class = UITextEffectsWindow

可能与window等级为10.0有关?当键盘最终出现时,window 级别为 1.0...

这里是 .m class 了解更多信息

    @interface addQuestion () <UITextViewDelegate>
    @property (weak, nonatomic) IBOutlet UITextView *questionText;
    @property (strong, nonatomic) NSDictionary *dictionary;

    @end

@implementation addQuestion

- (void)viewDidLoad {
    [super viewDidLoad];



    // Do any additional setup after loading the view.
    [self.questionText becomeFirstResponder];
    self.questionText.delegate = self;
    self.questionText.text = @"Ask a good yes or no question!";
    self.questionText.textColor = [UIColor lightGrayColor];

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Post" style:UIBarButtonItemStylePlain target:self action:@selector(posted)];
    self.navigationItem.rightBarButtonItem = anotherButton;


//    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
//        NSLog(@"isKeyWindow = %d window level = %.1f frame = %@ class = %@\n",
//              window.isKeyWindow, window.windowLevel,
//              NSStringFromCGRect(window.frame), window.class.description);
//    }
}

- (void)textViewDidChangeSelection:(UITextView *)textView{
    if ([textView.text isEqualToString:@"Ask a good yes or no question!"] && [textView.textColor isEqual:[UIColor lightGrayColor]])[textView setSelectedRange:NSMakeRange(0, 0)];

}

- (void)textViewDidBeginEditing:(UITextView *)textView{

    [textView setSelectedRange:NSMakeRange(0, 0)];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length != 0 && [[textView.text substringFromIndex:1] isEqualToString:@"Ask a good yes or no question!"] && [textView.textColor isEqual:[UIColor lightGrayColor]]){
        textView.text = [textView.text substringToIndex:1];
        textView.textColor = [UIColor blackColor]; //optional
        self.navigationItem.leftBarButtonItem.enabled=YES;


    }
    else if(textView.text.length == 0){
        self.navigationItem.leftBarButtonItem.enabled=NO;
        textView.text = @"Ask a good yes or no question!";
        textView.textColor = [UIColor lightGrayColor];
        [textView setSelectedRange:NSMakeRange(0, 0)];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"Ask a good yes or no question!";
        textView.textColor = [UIColor lightGrayColor]; //optional
    }
    //[textView resignFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (text.length > 1 && [textView.text isEqualToString:@"Ask a good yes or no question!"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }

    if([[textView text] length] > 140){
        return NO;
    } else {
        return YES;
    }
}
- (BOOL)canBecomeFirstResponder {
    return YES;
}  



@end
[self.questionText becomeFirstResponder];
remove this

我的问题是我试图在后台线程中进行 segue(在从 NSURLSession 回调之后)。

我应该做的是确保我在主线程中调用了 segue。

dispatch_async(dispatch_get_main_queue(), ^{
                [self performSegueWithIdentifier:@"proceed" sender:self];
            });