【发布时间】:2020-04-29 16:17:05
【问题描述】:
我有一个自定义 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 }
}
【问题讨论】:
-
不太清楚发生了什么...尝试为minimal reproducible example 提供足够的代码。
-
@DonMag 我添加了更多与 inputAccessoryView 相关的代码。在视觉上,我在视图控制器的底部添加了一个输入 textView,就像一个聊天室。我想切换显示/隐藏它,看起来 inputAccessoryView 有一个原生的底部滑入/滑出动画,我希望以编程方式调用它,但我不确定如何......乐意提供更多上下文!
标签: ios swift inputaccessoryview