ios键盘可点击但不可见
Posted
技术标签:
【中文标题】ios键盘可点击但不可见【英文标题】:ios keyboard clickable but not visible 【发布时间】:2015-01-13 12:02:35 【问题描述】:我正在制作一个允许用户提问的应用。在我的 addQuestion 类中,我有一个 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);
但没有覆盖 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
也许它与窗口级别为 10.0 有关?当键盘最终出现时,窗口级别为 1.0...
这里是 .m 类以获得更多信息
@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
【问题讨论】:
你用什么设备进行测试? 我的 iPhone 6 通过 xcode 运行 我的真实设备是......不是模拟器 @QKal 你有没有找到任何解决方案.. @AnkitSrivastava 是的,刚刚回答了 【参考方案1】:[self.questionText becomeFirstResponder];
remove this
【讨论】:
只强制用户手动按下文本视图,而不是自动输入。它没有使键盘可见。感谢您的快速响应【参考方案2】:我的问题是我试图在后台线程中进行 segue(在来自 NSURLSession 的回调之后)。
我应该做的是确保我在主线程中调用了 segue。
dispatch_async(dispatch_get_main_queue(), ^
[self performSegueWithIdentifier:@"proceed" sender:self];
);
【讨论】:
以上是关于ios键盘可点击但不可见的主要内容,如果未能解决你的问题,请参考以下文章