Скрытие / отображение вида аксессуара ввода - PullRequest
0 голосов
/ 29 апреля 2020

У меня есть пользовательский inputAccessoryView, и я пытаюсь его скрыть / показать. Я не хочу использовать .isHidden или .removeFromSuperView(), скорее, используйте нижний слайд in / out, который кажется родным, поскольку я представляю другие viewControllers в иерархии, и эта анимация выполняется.

Я попытался resignFirstResponder без удачи, и, кажется, нет никаких комментариев по этому поводу. Любые мысли будут оценены, поскольку я, по общему признанию, озадачен.

class CustomInputAccessoryView: UIView {

let customTextView: CustomInputTextView = {
    let tv = CustomInputTextView()
    tv.isScrollEnabled = false
    return tv
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.white
    autoresizingMask = .flexibleHeight

    addSubview(customTextView)
    customTextView.topAnchor.constraint(equalTo: topAnchor, constant: 12).isActive = true
    customTextView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: 8).isActive = true
    customTextView.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
    customTextView.rightAnchor.constraint(equalTo: rightAnchor, constant: 10).isActive = true
    customTextView.heightAnchor.constraint(greaterThanOrEqualToConstant: frame.height - 20).isActive = true
}

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

override var intrinsicContentSize: CGSize {
    return .zero
}
}

class CustomInputTextView: UITextView {
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    }

    override var canResignFirstResponder: Bool {
        return true
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

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

//in viewcontroller
lazy var inputContainerView: CustomInputAccessoryView = {
    let frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 60)
    let customInputAccessoryView = CustomInputAccessoryView(frame: frame)
    return customInputAccessoryView
}()        

//onLoad
override var inputAccessoryView: UIView? {
    get { return inputContainerView }
}

1 Ответ

0 голосов
/ 29 апреля 2020

Я не знаю, действительно ли это то, к чему вы стремитесь, но попробуйте.

Нажатие в любом месте представления отобразит / скроет представление вспомогательного ввода:

class TestInputViewController: UIViewController {

    //in viewcontroller
    lazy var inputContainerView: CustomInputAccessoryView = {
        let frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 60)
        let customInputAccessoryView = CustomInputAccessoryView(frame: frame)
        return customInputAccessoryView
    }()

    override var canBecomeFirstResponder: Bool {
        return true
    }

    //onLoad
    override var inputAccessoryView: UIView? {
        get { return inputContainerView }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // so we can see the frame
        inputContainerView.backgroundColor = .blue

        // tap anywhere in view to show / hide input accessory view
        let g = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
        view.addGestureRecognizer(g)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    @objc func didTap(sender: UITapGestureRecognizer) {
        if self.isFirstResponder {
            resignFirstResponder()
        } else {
            becomeFirstResponder()
        }
    }

}

class CustomInputAccessoryView: UIView {

    let customTextView: CustomInputTextView = {
        let tv = CustomInputTextView()
        tv.isScrollEnabled = false
        return tv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        autoresizingMask = .flexibleHeight

        addSubview(customTextView)
        customTextView.translatesAutoresizingMaskIntoConstraints = false
        customTextView.topAnchor.constraint(equalTo: topAnchor, constant: 12).isActive = true
        customTextView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -8).isActive = true
        customTextView.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
        customTextView.rightAnchor.constraint(equalTo: rightAnchor, constant: -10).isActive = true
        customTextView.heightAnchor.constraint(greaterThanOrEqualToConstant: frame.height - 20).isActive = true
    }

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

    override var intrinsicContentSize: CGSize {
        return .zero
    }
}

class CustomInputTextView: UITextView {
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    }

    override var canResignFirstResponder: Bool {
        return true
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

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

Не связано с отображением / скрытием, но некоторые из ваших ограничений были неправильными, из-за чего представление текста было смещено.

...