尝试以紧凑模式访问UITextView时,iMessage扩展崩溃 [英] iMessage extension crashes while trying to access UITextView in compact mode

查看:76
本文介绍了尝试以紧凑模式访问UITextView时,iMessage扩展崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在iMessage应用程序中的全部代码.

Below is the entirety of my code in an iMessage app.

class MessagesViewController: MSMessagesAppViewController {

@IBOutlet weak var messageView: UITextView!

fileprivate func setupMessageView() {
    messageView.delegate = self

    messageView.layer.cornerRadius = 10
    messageView.layer.borderColor = UIColor.black.cgColor
    messageView.layer.borderWidth = 5

    messageView.text = "Tap to enter a message"
    messageView.textColor = UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0)
    messageView.textAlignment = .center

    messageView.font = UIFont.systemFont(ofSize: 20)

    messageView.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

func initialize() {
    setupMessageView()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.initialize), userInfo: nil, repeats: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Conversation Handling

override func willBecomeActive(with conversation: MSConversation) {
    // Called when the extension is about to move from the inactive to active state.
    // This will happen when the extension is about to present UI.

    // Use this method to configure the extension and restore previously stored state.
}

override func didResignActive(with conversation: MSConversation) {
    // Called when the extension is about to move from the active to inactive state.
    // This will happen when the user dissmises the extension, changes to a different
    // conversation or quits Messages.

    // Use this method to release shared resources, save user data, invalidate timers,
    // and store enough state information to restore your extension to its current state
    // in case it is terminated later.
}

override func didReceive(_ message: MSMessage, conversation: MSConversation) {
    // Called when a message arrives that was generated by another instance of this
    // extension on a remote device.

    // Use this method to trigger UI updates in response to the message.
}

override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
    // Called when the user taps the send button.
}

override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
    // Called when the user deletes the message without sending it.

    // Use this to clean up state related to the deleted message.
}

override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    // Called before the extension transitions to a new presentation style.

    // Use this method to prepare for the change in presentation style.
}

override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    // Called after the extension transitions to a new presentation style.

    // Use this method to finalize any behaviors associated with the change in presentation style.
}

}

extension MessagesViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
    if textView == messageView {
        requestPresentationStyle(.expanded)
        if textView.textColor == UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0) {
            textView.text = nil
            textView.textAlignment = .left
            textView.textColor = UIColor.black
            textView.font = UIFont.systemFont(ofSize: 20)
        }
    }
}

func textViewDidEndEditing(_ textView: UITextView) {
    if textView == messageView {
        if textView.text.isEmpty {
            textView.text = "Tap to enter a message"
            textView.textAlignment = .center
            textView.textColor = UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0)
            textView.font = UIFont.systemFont(ofSize: 20)
        }
    }
}
}

它有一个UITextView,我尝试输入数据.我在执行此操作时遇到了一个奇怪的问题.

It has a UITextView, and I try enter data. I experience a weird problem while performing this action.

在初始加载时,如果我点击UITextView,则会调用扩展模式,但键盘不会向上滑动.需要再次轻按才能使键盘向上滑动.

On initial load, if I tap the UITextView, expanded mode is called, but the keyboard doesn't slide up. It needs another tap for the keyboard to slide up.

在日志中,我发现,第一次点击时会依次调用textViewDidBeginEditingtextViewDidEndEditing方法.不确定,为什么会这样发生!?

In the logs, I was able to find, that the textViewDidBeginEditing and textViewDidEndEditing methods are called successively on the first tap. Not sure, why it is happening this way!?

无论如何,更让我着迷的是现在发生的事情.我可以将模式更改为手动压缩,然后再返回展开.如果处于扩展模式,则一旦我点击,键盘就会向上滑动.但是,如果我在紧凑模式下点击,则应用程序将崩溃!

Anyways, what intrigues me more, is what happens now. I can change the mode to compact manually, and back to expanded. If in expanded mode, once I tap, the keyboard slides up. But, if I tap while in compact mode, the app crashes!!

而且这种情况一直都在发生.在模拟器和真实设备上.我没有任何线索可以解释这种行为.

And this happens all the time. On simulator and a real device. I have no clue to explain this behaviour.

无论我将模式从压缩模式更改为扩展模式再返回多少次,我都可以在扩展模式下输入文本.但是,在紧凑模式下,第一次点击后再也不会发生这种情况.

No matter how many times I change the mode from compact to expanded and back, I can enter text in expanded mode. But, after the first tap, it never happens again, while in the compact mode.

有人遇到这个问题吗?还是可以复制这个?这是Apple的错误吗?

Does anyone have this issue? Or can you replicate this? Is this a bug wth Apple?

推荐答案

如果在用户点击文本字段后需要更改演示样式,则可以添加延迟以确保与过渡没有冲突.

If you need to change the presentation style after the user taps in the textfield, you could add a delay to ensure that there are no clashes with the transitions.

最好先阻止键盘显示(您可能需要在VC中设置一个bool标志,才能在textview委托中将其关闭以启用此功能),然后调用

It maybe better to initially prevent the keyboard from showing (You may need to set a bool flag in your VC to switch on off in your textview delegates to enable this), call

  func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    if  !shouldShowKeyBoard {
        self.requestPresentationStyle(.expanded)
        return false
    }
    return true

然后在MSMessagesAppViewController委托中使键盘成为第一响应者 `

Then in the MSMessagesAppViewController delegate enable the keyboard to become first responder `

 override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    if presentationStyle == .expanded {
       shouldShowKeyBoard = true
       textView.becomeFirstResponder()
    }
}

`

希望这会有所帮助

这篇关于尝试以紧凑模式访问UITextView时,iMessage扩展崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆