无法输入到inputAccessoryView的文本字段

时间:2016-01-05 02:27:13

标签: ios swift cocoa-touch

我创建了一个输入附件视图,代码如下:

import UIKit

class ChatInputView: UIView {
    let textView: ChatTextView = {
        let textView = ChatTextView()
        textView.translatesAutoresizingMaskIntoConstraints = false

        textView.keyboardType = .Default
        textView.returnKeyType = .Send
        textView.font = UIFont.systemFontOfSize(15.0)

        return textView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        commonInit()
    }

    func commonInit() {
        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = UIColor.whiteColor()

        addSubview(textView)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewNeedsUpdateDisplay", name: UITextViewTextDidChangeNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewNeedsUpdateDisplay", name: UITextViewTextDidBeginEditingNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewNeedsUpdateDisplay", name: UITextViewTextDidEndEditingNotification, object: nil)

        textView.positionToSuperView(horizontal: UIView.Horizontal.Left, hOffset: 15.0, vertical: UIView.Vertical.Top, vOffset: 15.0)
        textView.positionToSuperView(horizontal: UIView.Horizontal.Right, hOffset: 15.0, vertical: UIView.Vertical.Bottom, vOffset: 15.0)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    override func intrinsicContentSize() -> CGSize {
        let height = textView.intrinsicContentSize().height + 30.0 > UIScreen.mainScreen().bounds.height / 3.0 ? UIScreen.mainScreen().bounds.height / 3.0 : textView.intrinsicContentSize().height + 30.0
        let width = UIScreen.mainScreen().bounds.width

        return CGSize(width: width, height: height)
    }

    func textViewNeedsUpdateDisplay() {
        textView.setNeedsDisplay()
        invalidateIntrinsicContentSize()
    }

}

class ChatTextView: UITextView {
    override var text: String! {
        didSet {
            setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        if text.characters.count == 0 {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .ByTruncatingTail
            paragraphStyle.alignment = .Left

            let placeholderTextAttributes: [String: AnyObject] = [
                NSFontAttributeName: UIFont.systemFontOfSize(15.0),
                NSForegroundColorAttributeName: UIColor.lightGrayColor(),
                NSParagraphStyleAttributeName: paragraphStyle
            ]

            (NSLocalizedString("输入一些字来回复...", comment: "") as NSString).drawInRect(CGRectInset(rect, 7, 8), withAttributes: placeholderTextAttributes)
        }
    }

    override func intrinsicContentSize() -> CGSize {
        return sizeThatFits(CGSize())
    }

}

并按以下方式使用

override var inputAccessoryView: UIView! {
    get {
        return ChatInputView()
    }
}

但是当我点击文本字段时,键盘可以显示,但无法输入到textfield.Tried我的方法,但仍然无法让它工作。

任何人都可以告诉我原因。

0 个答案:

没有答案