简体   繁体   English

如何以编程方式在 UIView 内部创建一个以编程方式添加的 UITextView?

[英]How to create a UITextView programatically, inside UIView that is added programatically too?

I am trying to make a text acceptor view like Instagram and Snapchat.我正在尝试制作像 Instagram 和 Snapchat 这样的文本接受器视图。

I am trying to add a UITextview (programatically) inside a UIView thats also added programatically.我正在尝试在 UIView 中添加一个 UITextview(以编程方式),这也是以编程方式添加的。 Below is the code I am trying to use to add the UITextView, but it is not working when added with UIView code.下面是我试图用来添加 UITextView 的代码,但在添加 UIView 代码时它不起作用。 Please help.请帮忙。 当我将文本视图与 uiview 一起添加时,它就是这样显示的 What I am trying to achieve is:我想要实现的是: 我正在尝试在上一张图片的红色 uiview 中实现此文本视图 import UIKit进口 UIKit

    class TextViewViewController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let View1: UIView = {
                let viewView = UIView()
                viewView.translatesAutoresizingMaskIntoConstraints = false
                viewView.contentMode = .scaleAspectFit
                viewView.backgroundColor = .red
                viewView.clipsToBounds = true
                return viewView
            }()
    
            self.view.addSubview(View1)
    
            View1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
            View1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
            View1.topAnchor.constraint(equalTo: view.topAnchor, constant: 250).isActive = true
            View1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -250).isActive = true
            
            let textView = UITextView(frame: CGRect(x: 20.0, y: 90.0, width: 250.0, height: 100.0))
            textView.contentInsetAdjustmentBehavior = .automatic
             
             textView.center = self.view.center
             textView.textAlignment = NSTextAlignment.justified
             textView.backgroundColor = UIColor.lightGray
             
             // Use RGB colour
             textView.backgroundColor = UIColor(red: 39/255, green: 53/255, blue: 182/255, alpha: 1)
             
             // Update UITextView font size and colour
             textView.font = UIFont.systemFont(ofSize: 20)
             textView.textColor = UIColor.white
             textView.font = UIFont(name: "Verdana", size: 17)
  
             // Make UITextView web links clickable
             textView.isSelectable = true
             textView.isEditable = false
             textView.dataDetectorTypes = UIDataDetectorTypes.link
             
             // Make UITextView corners rounded
             textView.layer.cornerRadius = 10

             // Make UITextView Editable
             textView.isEditable = true
            View1.addSubview(textView)
//
//        let textView: UITextView = {
//            let tv = UITextView(frame: CGRect(x: 20.0, y: 90.0, width: 100.0, height: 50.0))
//            tv.contentInsetAdjustmentBehavior = .automatic
//            tv.center = self.view.center
//            tv.textAlignment = NSTextAlignment.justified
//            tv.textColor = UIColor.blue
//            tv.backgroundColor = UIColor.lightGray
//            return tv
//        }()
//
//        View1.addSubview(textView)
        
//        textView.topAnchor.constraint(equalTo: View1.topAnchor, constant: 20).isActive = true
//        textView.bottomAnchor.constraint(equalTo: View1.bottomAnchor, constant: 20).isActive = true
//        textView.leadingAnchor.constraint(equalTo: View1.leadingAnchor, constant: 0).isActive = true
//        textView.trailingAnchor.constraint(equalTo: View1.trailingAnchor, constant: 0).isActive = true
    }
}

The issue is in the next line of code问题出在下一行代码中

textView.center = self.view.center

Get rid of it and you will see the textView in the view hierarchy摆脱它,您将在视图层次结构中看到 textView

在此处输入图像描述

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

相关问题 如何使用XCTest在以编程方式添加的UIView上测试UILabel - How to test a UILabel on a programatically added UIView with XCTest 如何以编程方式将 UIStackview 放入 Swift 中的 UIView 中 - How to put a UIStackview inside a UIView in Swift programatically 自动调整大小不适用于以编程方式添加的UIView - Autoresizing not working for UIView added programatically 滚动UITextView以显示以编程方式添加的新文本 - Scroll UITextView to Show New Text Added Programatically Swift 如何以编程方式将垂直 UIStackview / UIView 放入水平 UIStackview - Swift how to put vertical UIStackview / UIView inside horizontal UIStackview programatically 如何以编程方式在其中创建带有UIWebView的UINavigationController? - How to create a UINavigationController with a UIWebView inside of it programatically? 快速在UITableViewCell内以编程方式添加UIViewController或UIView - Swift Add UIViewController or UIView inside UITableViewCell programatically UITextView如何以编程方式编辑某些字符串? - UITextView how edit some strings programatically? 以编程方式创建自定义UIView子类的对象 - Create object of custom UIView subclass programatically 如何以编程方式从xib更改UITextView的高度? - How to change height of UITextView from xib programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM