如何设置 stackview 和 tableviewcell 内 textview 的动态高度?以快速编程方式

问题描述 投票:0回答:1

我正在以编程方式开发待办事项应用程序,但我不知道该怎么做。请帮忙。 我已经完成了它的每一个解决方案。 (isScrollEnabled = false, .sizetofit(), translaste~ =true ...)

如何设置 stackview 和 tableviewcell 内 textview 的动态高度? 我想要textview的基本高度大于50,然后动态改变textview高度。

我在下面附上了我的代码。

class ToDoTableViewCell: UITableViewCell, UITextViewDelegate {
    
    lazy var backColorView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    let memoString: UITextView = {
        let textView = UITextView()
        textView.font = .systemFont(ofSize: 17, weight: .regular)
        textView.layer.cornerRadius = 0
        textView.layer.borderWidth = 1
        textView.layer.borderColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        textView.backgroundColor = .clear
        textView.isS
        return textView
    }()
    
    let dateString: UILabel = {
        let date = UILabel()
        date.font = .systemFont(ofSize: 14, weight: .light)
        date.text = "2021-11-12"
        return date
    }()
    
    let updateButton: UIButton = {
        let button = UIButton()
        button.setTitle("UPDATE", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 9, weight: .bold)
        button.layer.cornerRadius = 10
        button.backgroundColor = .gray
        button.setImage(UIImage(systemName: "pencil"), for: .normal)
        return button
    }()
    
    let totalStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.distribution  = .fill
        stack.alignment = .fill
        stack.spacing = 10
        return stack
    }()
    
    let subStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.distribution  = .fill
        stack.alignment = .fill
        stack.spacing = 0
        return stack
    }()
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        memoString.delegate = self

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        setupStackView()
        setConstraints()
        

    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupStackView() {
        
        self.contentView.addSubview(backColorView)
        
        totalStackView.addArrangedSubview(memoString)
        totalStackView.addArrangedSubview(subStackView)
        
        subStackView.addArrangedSubview(dateString)
        subStackView.addArrangedSubview(updateButton)
        
        self.contentView.addSubview(totalStackView)
        
    }
    
    
    func setConstraints() {
        totalStackView.translatesAutoresizingMaskIntoConstraints = false
        subStackView.translatesAutoresizingMaskIntoConstraints = false
        updateButton.translatesAutoresizingMaskIntoConstraints = false
        memoString.translatesAutoresizingMaskIntoConstraints = false
       
        
        
        
        
        NSLayoutConstraint.activate([
            
            totalStackView.leadingAnchor.constraint(equalTo: backColorView.leadingAnchor, constant: 10),
            totalStackView.trailingAnchor.constraint(equalTo: backColorView.trailingAnchor, constant: -10),
            totalStackView.topAnchor.constraint(equalTo: backColorView.topAnchor, constant: 10),
            totalStackView.bottomAnchor.constraint(equalTo: backColorView.bottomAnchor, constant: -10),
            
            
            subStackView.heightAnchor.constraint(equalToConstant: 30),
            updateButton.widthAnchor.constraint(equalToConstant: 80),
            
            backColorView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 25),
            backColorView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -25),
            backColorView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10),
            backColorView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10),
            
           memoString.heightAnchor.constraint(greaterThanOrEqualToConstant: 50)
           
 
        ])
    }
    

    
    
}
class ViewController: UIViewController {

    private let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        subviews()
        constraints()
        setupTableView()
        makeUI()
        makeButton()
    }

    func setupTableView() {
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.register(ToDoTableViewCell.self, forCellReuseIdentifier: "cell")
    }
}

extension ViewController {
    
    func makeUI() {
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithOpaqueBackground()
            navigationController?.navigationBar.standardAppearance = navigationBarAppearance
            navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
            navigationController?.navigationBar.tintColor = .blue

            navigationItem.scrollEdgeAppearance = navigationBarAppearance
            navigationItem.standardAppearance = navigationBarAppearance
            navigationItem.compactAppearance = navigationBarAppearance

            navigationController?.setNeedsStatusBarAppearanceUpdate()
                    

            navigationController?.navigationBar.isTranslucent = false
            navigationController?.navigationBar.prefersLargeTitles = true
            //navigationController?.navigationBar.backgroundColor = .white
            title = "메모"
        }
    
    func makeButton() {
            let button = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(action))
            navigationItem.rightBarButtonItem = button
        }
    
    @objc func action() {
        
    }
    
    func subviews() {
        view.addSubview(tableView)
    }

    func constraints() {
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
            tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        return cell
    }
 


}

我想要textview的基本高度大于50,然后动态改变textview高度。

isScrollEnabled = false, .sizetofit(), translaste~ =true ...

swift uitableview textview stackview
1个回答
0
投票

创建一个变量

 var textViewHeightCon:NSLayoutConstraint!

然后从

activate

中取出以下行
textViewHeightCon = memoString.heightAnchor.constraint(greaterThanOrEqualToConstant: 50)
textViewHeightCon.isActive = true

然后在需要更改的地方执行此操作

textViewHeightCon.constant = <#Value#>
self.layoutIfNeeded()
© www.soinside.com 2019 - 2024. All rights reserved.