簡體   English   中英

如何以編程方式在 UIView 內部創建一個以編程方式添加的 UITextView?

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

我正在嘗試制作像 Instagram 和 Snapchat 這樣的文本接受器視圖。

我正在嘗試在 UIView 中添加一個 UITextview(以編程方式),這也是以編程方式添加的。 下面是我試圖用來添加 UITextView 的代碼,但在添加 UIView 代碼時它不起作用。 請幫忙。 當我將文本視圖與 uiview 一起添加時,它就是這樣顯示的 我想要實現的是: 我正在嘗試在上一張圖片的紅色 uiview 中實現此文本視圖 進口 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
    }
}

問題出在下一行代碼中

textView.center = self.view.center

擺脫它,您將在視圖層次結構中看到 textView

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM