简体   繁体   English

如何在Swift的文本视图旁边添加按钮(以编程方式或在情节提要中)?

[英]how can I add a button (programatically or in storyboard) right next to my text view in Swift?

I'm writing a swift ios app and I introduced this textView : https://github.com/KennethTsang/GrowingTextView 我正在编写一个swift ios应用程序,并介绍了此textViewhttps : //github.com/KennethTsang/GrowingTextView

In my code I added InputToolbar with one item: 在我的代码中,我在InputToolbar添加了一项:

在此处输入图片说明

and then included the code: 然后包含代码:

    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)

so now there's a full-width textview there. 所以现在那里有一个全角textview How could I add a button right next to the text view? 如何在文本视图旁边添加按钮? For example in whatsapp there's this small microphone next to the textview : 例如,在whatsapp中, textview旁边有一个小麦克风:

在此处输入图片说明

Ok, here's how you add a UITextView with additional buttons to a UIToolBar: 好的,这是将带有附加按钮的UITextView添加到UIToolBar的方法:

You add a UIView to the UIToolBar . 您将UIView添加到UIToolBar Afterwards you can add a UITextView to the UIView that resides in the UIToolBar . 之后,您可以将UITextView添加到位于UIToolBarUIView中。

Then you can set the Custom Class of the UITextView to GrowingTextView ( https://github.com/KennethTsang/GrowingTextView ). 然后,您可以将UITextView的Custom Class设置为GrowingTextViewhttps://github.com/KennethTsang/GrowingTextView )。

Here's a screenshot of my storyboard. 这是我的故事板的屏幕截图。

UIToolBar内具有其他按钮的GrowingTextView

Let me know if that helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS - 以编程方式添加新视图以使用我的storyboard autolayout viewcontroller - iOS - Programatically add a new view to work with my storyboard autolayout viewcontroller 无法以编程方式将视图添加到情节提要中的现有子视图 - Can't add a view to an existing subview in storyboard programatically 如何快速在故事板对象之前以编程方式添加对象 - How can I add object programmatically before storyboard objects in swift 如何添加一个覆盖故事板序列的按钮(快速3) - how to add a button that overrides a storyboard segue (in swift 3) 如何在Swift 2中以编程方式向按钮添加约束? - How to programatically add constraints to a button in Swift 2? 如何以编程方式快速将子视图添加到自定义视图 - How to add a subview to a custom view programatically in swift 如何将新视图添加到情节提要中? - How do I add my new View to the storyboard? 如何在情节提要板上添加分段控件视图? - How can I add a segmented control view on the storyboard? 我可以在我的视图中添加一个计时器以进入下一个视图吗? - Can I add a timer to my view to segue to next view? 如何清除Swift中导航后退按钮旁边的文本? - How can I get rid of the text next to the navigation back button in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM