iOS共享扩展解除键盘

r8xiu3jd  于 2023-05-23  发布在  iOS
关注(0)|答案(3)|浏览(201)

我正在为我的应用程序实现一个共享扩展,到目前为止,一切都很顺利,除了我似乎无法关闭使用默认布局/故事板自动打开的键盘。
我保留了默认的设计/布局(SLComposeServiceViewController),其中包括预览图像和UITextview,UITextview自动获得焦点,打开键盘。
通常情况下,这是好的,但如果你没有登录我的应用程序,我显示一个UIAlertController说,你需要登录共享。问题是键盘与警报同时打开。
我在viewDidLoad、viewDidAppear和viewWillAppear中都试过[self.view endEditing:YES];[self.textView resignFirstResponder];,但没有成功。

x6h2sr28

x6h2sr281#

找到答案了!我没有仔细阅读文档...
我必须在-(void)presentationAnimationDidFinish中执行[self.textView resignFirstResponder];

fwzugrvs

fwzugrvs2#

我的方法是使用UITextViewDelegate

- (void)viewDidLoad {
     [super viewDidLoad];
     self.textView.delegate = self;
     self.canShare = NO;
     [self.view setAlpha:0.0];
}

在检查登录逻辑中将canShare更改为YES

- (void)checkLoggedIn {
    if ([[ShareAccountManager checkLoggedIn]) {
        self.canShare = YES;
        [self.view setAlpha:1.0];
    }
}

并实现方法textViewShouldBeginEditing

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (self.canShare) {
        return YES;
    }
    return NO;
}
zd287kbt

zd287kbt3#

如果您使用的是默认的UI和MainInterface,那么在这个故事板中,您应该能够看到它是“FirstResponder”

如果是这种情况,在viewWillAppear内部调用self.resignFirstResponder()将关闭键盘。

相关问题