- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我已经花了几天时间解决这个问题,看不到任何解决方案。
我有一个 inputAccessoryView
,它由一个包含 textView
和两个按钮的 UIView
组成。 inputAccessoryView
的行为符合预期,并且在除一种情况外的所有情况下都可以正常工作。
当 textView 的高度增加时,我试图将 inputAccessoryView
的高度增加相同的量。当我在 textViewDidChange
中重新定义 inputAccessoryView
的高度时,inputAccessoryView
会在键盘上向下而不是向上增加高度。
我尝试了 SO 的许多不同建议,但没有任何效果。我猜这是 inputAccessoryView
自动添加的 NSLayoutConstraint
但我不知道如何在 swift 和 iOS 8.3 中更改该值。
func textViewDidChange(textView: UITextView) {
var contentSize = messageTextView.sizeThatFits(CGSizeMake(messageTextView.frame.size.width, CGFloat.max))
inputAccessoryView.frame.size.height = contentSize.height + 16
}
添加
inputAccessoryView.setTranslatesAutoresizingMaskIntoConstraints(true)
对上面的代码有帮助并且 inputAccessoryView 高度正确地向上增加但是我得到无法同时满足多个约束的约束并且很难识别违规者。此外,我还得到一个奇怪的效果,即 textView 在每两个新行的实例下方创建额外的空间。
谢谢。
最佳答案
要使输入附件 View 垂直增长,您只需设置它的 autoresizingMask = .flexibleHeight
,计算它的 intrinsicContentSize
,然后让框架完成剩下的工作。
代码:
class InputAccessoryView: UIView, UITextViewDelegate {
let textView = UITextView()
override init(frame: CGRect) {
super.init(frame: frame)
// This is required to make the view grow vertically
self.autoresizingMask = UIView.AutoresizingMask.flexibleHeight
// Setup textView as needed
self.addSubview(self.textView)
self.textView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))
self.textView.delegate = self
// Disabling textView scrolling prevents some undesired effects,
// like incorrect contentOffset when adding new line,
// and makes the textView behave similar to Apple's Messages app
self.textView.isScrollEnabled = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
// Calculate intrinsicContentSize that will fit all the text
let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: self.bounds.width, height: textSize.height)
}
// MARK: UITextViewDelegate
func textViewDidChange(_ textView: UITextView) {
// Re-calculate intrinsicContentSize when text changes
self.invalidateIntrinsicContentSize()
}
}
关于ios - 如何增加 inputAccessoryView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31822504/
我正在尝试在 SwiftUI 中的 TextField 上实现一个 inputAccessoryView。目标是在键盘上方出现一个“完成”按钮,按下该按钮时会摆脱键盘(即 resignFirstRes
我已添加 InputAccessoryView到我的键盘消失。它炒得很好。但在某些 ViewController它没有出现。即使那个方法也不会调用。 我有一个按钮。当我点击我的文本字段时,它会自动成为
自从我升级到 Xcode 5.1 后,我的 inputAccessoryView 出现了问题。 如您在附图中所见,按键在 inputAccessoryView 内的工具栏后面弹出。我认为它与新版本的
我正在为 UITextField 和 UIToolbar 设置一个 datepicker 来代替键盘。我希望能够关闭 accessoryView,但它没有正常运行。虽然我按下了 Done 按钮,但没有
我有一个 Objective-C 项目,我想在其中添加一个 inputAccessoryView 到 UITextView 的子类。老办法是 self.myTextView.inputAccessor
我已经看过了this solution .我已经创建了一个显示错误的示例项目。 该 View 只有一个 UITextField。 这是我的代码: class ViewController: UIVie
Problem relates to this.我正在尝试使用我制作的自定义 View ,它有两个 TextField 输入和一个按钮,我使用 IB 它的 xib 制作它,我将它放在我的 Storyb
今天早上,我正在查看我在某个地方找到的一些旧代码(并在应用程序中使用),这些代码破解了 UIAlertView 以便从用户那里获取一串输入。出于这个目的,它有点矫枉过正,看起来很傻,但我从未见过更简单
我正在尝试复制 Facebook Messenger 应用程序,其中有一个 UITextView 附加到键盘顶部。 由于此应用的性质,我需要附加我的 View ,而不是在出现键盘时手动上下滚动Scro
是否可以在键盘上方创建 inputAccessoryView,用于拆分模式下的屏幕全宽?系统会自动调整其大小以适合应用程序框架。这是 iOS 中的错误吗? 最佳答案 您可以使用此方法检测您的键盘是否处
知道如何让 inputAccessoryView 锚定到标签栏而不是屏幕底部吗? 我创建了一个 UIViewController 并覆盖了以下方法: -(BOOL)canBecomeFirstResp
我正在尝试创建一个类似于 iOS 消息传递应用程序的消息传递界面。我正在努力解决的部分是覆盖 inputAccessoryView(在 View Controller 上)对我没有影响。 如果您查看下
我正在尝试添加带有“完成”按钮的 UIView 作为文本字段的输入附件 View 。 let view = UIView() let doneButton = UIBu
UISearchBar 似乎将 inputAccessoryView 作为 readOnly 属性。如何使用我自己的 customToolbar 进行设置? 最佳答案 编辑:如以下评论中所述,这不再是
我在 textView 的 inputAccessoryView 属性中使用工具栏。当键盘显示时,它会按预期显示工具栏。旋转设备时,我想删除工具栏。我试过: myTextView.inputAcce
我正在尝试实现与 Apple 消息应用中底部文本输入栏类似的定位行为。 我尝试了很多方法,到处搜索,有很多类似的问题,但没有一个是令人满意的。 要指定: 在 View 底部有一个UIToolbar 工
我已经为我的应用程序中的某些 UITextFields 添加了一个 inputAccessoryView 到 iOS 键盘。这个附件 View 为用户提供了基本功能(一些按钮在我的应用程序的其他地方没
我正在以模态方式呈现一个 UIViewController,该 View 包含各种 UITextFields。我已经重写了模态视图 InputAccessoryView,以便它显示一个简单的 View
全部, 我的 UITextView 上附加了一个可滚动的 InputAccessoryView。创建如下: 创建一个 UIScrollView 添加一个水平 UIStackView。 将堆栈 View
我有一个简单的 UIViewController,其中包含一些对象:一个 UITextView 和一个作为 InputAccessoryView 添加到 UTextView 的 UIToolBar。当
我是一名优秀的程序员,十分优秀!