ios - 如何在 Swift 中的 TextView 旁边添加一个按钮(以编程方式或在 Storyboard 中)?

标签 ios swift storyboard nslayoutconstraint

我正在编写一个 swift ios 应用程序,我引入了这个 textView:https://github.com/KennethTsang/GrowingTextView

在我的代码中,我添加了带有一项的 InputToolbar:

enter image description here

然后包含代码:

    let textView = GrowingTextView()
    textView.delegate = self
    textView.layer.cornerRadius = 4.0
    textView.maxLength = 200
    textView.maxHeight = 70
    textView.trimWhiteSpaceWhenEndEditing = true
    textView.placeHolder = "Write a comment..."
    textView.placeHolderColor = UIColor(white: 0.8, alpha: 1.0)
    textView.placeHolderLeftMargin = 5.0
    textView.font = UIFont.systemFontOfSize(15)

    inputToolbar.translatesAutoresizingMaskIntoConstraints = false
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.returnKeyType = UIReturnKeyType.Send
    inputToolbar.addSubview(textView)
    let views = ["textView": textView]
    let hConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[textView]-8-|", options: [], metrics: nil, views: views)
    let vConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[textView]-8-|", options: [], metrics: nil, views: views)
    inputToolbar.addConstraints(hConstraints)
    inputToolbar.addConstraints(vConstraints)

所以现在那里有一个全宽的textview。我怎样才能在 TextView 旁边添加一个按钮?例如,在 whatsapp 中,textview 旁边有一个小麦克风:

enter image description here

最佳答案

好的,这是将带有附加按钮的 UITextView 添加到 UIToolBar 的方法:

您将 UIView 添加到 UIToolBar。之后,您可以将 UITextView 添加到 UIToolBar 中的 UIView

然后您可以将 UITextView 的自定义类设置为 GrowingTextView ( https://github.com/KennethTsang/GrowingTextView )。

这是我的 Storyboard的屏幕截图。

GrowingTextView inside UIToolBar with additional buttons

如果有帮助,请告诉我。

关于ios - 如何在 Swift 中的 TextView 旁边添加一个按钮(以编程方式或在 Storyboard 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812161/

相关文章:

ios - UITableView 重新加载数据有效,但单元格不会在屏幕上更新

arrays - 如何在 Swift 中将 int 数组分配给对象数组的相应属性?

ios - 如何在不重置位置的情况下更改UIButton?

iphone - 如何使用 Storyboard呈现启动/登录 View Controller

swift - SwinjectStoryboard - 是否可以加载/注入(inject)其 ViewController 存在于另一个项目的不同框架中的 Storyboard?

ios - Paypal IPN 模拟器和沙盒帐户

iOS:dequeueReusableCell(withIdentifier:for:) 和 dequeueReusableCell(withIdentifier:) 有什么区别?

ios - 在多个 View 之一中关闭键盘

ios - UITest 无法终止 com.test.abc :3708 after 60. 0s;状态仍然是`Running Foreground

ios - 使用一个 Storyboard和一个 Storyboard将 iPad 应用程序移植到 iPhone