iOS Как сделать текстовое представление, которое в inputaccessory вид первого респондента? - PullRequest
0 голосов
/ 11 июня 2018

Я хочу текстовое представление в дополнительном представлении, но вспомогательное представление должно быть видно только тогда, когда я нажимаю кнопку в представлении прокрутки, и клавиатура должна интерактивно закрываться с отображением текста при пролистывании.Что такое решение?

Facebook messenger & WhatsApp делает то же самое, но у них вид аксессуара, видимый даже при закрытой клавиатуре, но я этого не хочу.

Мое решениеЯ сделал вспомогательный вид прозрачным, а текстовое представление является подпредставлением scrollview, и я расположу текстовое представление прямо над прозрачным вспомогательным представлением, чтобы оно выглядело как вспомогательное представление.Но когда я делаю это, текстовое представление становится недоступным, так как окно вспомогательного просмотра находится над ним.

Мой код:

class ViewController: UIViewController, CustomKeyboardProtocol {

let textView = UITextView()
let button = UIButton()
let scrollView = UIScrollView()

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = .black

    scrollView.contentSize = CGSize(width: 10000, height: 10000)
    scrollView.keyboardDismissMode = .interactive
    scrollView.backgroundColor = .blue
    self.view.addSubview(scrollView)

    button.backgroundColor = .green
    button.frame = CGRect(x: 20, y: 20, width: 30, height: 30)
    button.addTarget(self, action: #selector(keyboard), for: .touchUpInside)
    scrollView.addSubview(button)

    textView.backgroundColor = .yellow
    textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40)
    self.view.addSubview(textView)
    textView.isHidden = true

    let accessoryView = CustomKeyboardAccessoryView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    accessoryView.keyboardDelegate = self
    accessoryView.backgroundColor = .clear
    textView.inputAccessoryView = accessoryView
}

func keyboardFrameChanged(frame: CGRect) {
    textView.frame = CGRect(x: 0, y: frame.origin.y, width: UIScreen.main.bounds.size.width, height: 40)
}

@objc func keyboardWillShow(notification: Notification) {
    if let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardHeight = keyboardFrame.size.height
        print("keyboard height: \(keyboardHeight)")
        textView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - keyboardHeight, width: UIScreen.main.bounds.size.width, height: 40)
    }
}

@objc func keyboardWillHide() {
    textView.isHidden = true
    textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0)
}

@objc func keyboard() {
    if textView.isFirstResponder {
        textView.resignFirstResponder()
        textView.isHidden = true
    } else {
        textView.becomeFirstResponder()
        textView.isHidden = false
    }
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    scrollView.frame = UIScreen.main.bounds
}

override var canBecomeFirstResponder: Bool {
    return true
}
}

protocol CustomKeyboardProtocol : NSObjectProtocol {
    func keyboardFrameChanged(frame : CGRect)
}

class CustomKeyboardAccessoryView: UIView {



weak var keyboardDelegate: CustomKeyboardProtocol? = nil

override func willMove(toSuperview newSuperview: UIView?) {
    if newSuperview == nil {
        self.superview?.removeObserver(self, forKeyPath: "center")
    }
    else{
        newSuperview?.addObserver(self, forKeyPath: "center", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
    }
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let theChange = change as [NSKeyValueChangeKey : AnyObject]? {
        if theChange[NSKeyValueChangeKey.newKey] != nil {
            if self.keyboardDelegate != nil && self.superview?.frame != nil {
                self.keyboardDelegate?.keyboardFrameChanged(frame: (self.superview?.frame)!)
            }
        }
    }


   }
}

Ответы [ 2 ]

0 голосов
/ 22 июля 2018

OSX: мне нужно было текстовое поле и его функции - легкий выбор и т. Д., Поэтому я делаю это:

fileprivate class URLField: NSTextField {
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        if let textEditor = currentEditor() {
            textEditor.selectAll(self)
        }
    }

    convenience init(withValue: String?) {
        self.init()

        if let string = withValue {
            self.stringValue = string
        }
        self.lineBreakMode = NSLineBreakMode.byTruncatingHead
        self.usesSingleLineMode = true
    }

    override func viewDidMoveToWindow() {
        // MARK: this gets us focus even when modal
        self.becomeFirstResponder()
    }
}

и использую так:

    // Create urlField
    let urlField = URLField(withValue: "It's a small world after all")
    urlField.frame = NSRect(x: 0, y: 0, width: 300, height: 20)
    alert.accessoryView = urlField
0 голосов
/ 12 июня 2018

Один обходной путь мог бы быть: Используйте свойство textView, чтобы просто переключать клавиатуру, но скрыть ее в методе клавиатуры, подобном этому

  @objc func keyboard() {
        if textView.isFirstResponder {
            textView.resignFirstResponder()
            textView.isHidden = true
        } else {
            textView.becomeFirstResponder()
            textView.isHidden = true
        }
    }

и создать отдельный view customView и добавить его в inputAccessoryView textView (textView упомянуто выше)

 textView.inputAccessoryView = customViewTV

внутри customViewTV добавить отдельный textView в качестве подпредставления и использовать это

   func createCustomTV(text: String) {
        customViewTV = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))
        customViewTV.backgroundColor = UIColor.red
        let acceTextView = UITextView()
        acceTextView.text = text
        acceTextView.frame = CGRect(x: 0, y: 0, width: customViewTV.frame.width, height: customViewTV.frame.height)
        acceTextView.center = customViewTV.center
        acceTextView.textColor = .black
        customViewTV.addSubview(acceTextView)
    }

Результат

enter image description here

...