首页 > 解决方案 > 隐藏/显示输入附件视图

问题描述

我有一个自定义inputAccessoryView,正在尝试切换隐藏/显示它。我不想使用,.isHidden或者.removeFromSuperView()更确切地说,使用底部的滑入/滑出,这似乎是原生的,因为我将其他内容呈现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 }
}

标签: iosswiftinputaccessoryview

解决方案


我不知道这是否真的是你想要的,但试试看。

点击视图中的任意位置将显示/隐藏输入附件视图:

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")
    }
}

与显示/隐藏无关,但您的一些约束是错误的,导致文本视图放错了位置。


推荐阅读