Swift - 在标签中动态调整大小后动态调整视图大小

Posted

技术标签:

【中文标题】Swift - 在标签中动态调整大小后动态调整视图大小【英文标题】:Swift - Dynamically resize view after label in it resizes dynamically 【发布时间】:2020-04-09 03:36:48 【问题描述】:

我创建了一个包含图像和标签的自定义视图。我在上面添加了一个点击手势,所以当用户点击视图时,标签和自定义视图都会展开以在标签上显示详细信息/更多文本。

我添加了代码来展开工作正常的标签,但 UIView/自定义视图没有得到展开。下面是我扩展标签的代码。

如何以编程方式或通过添加任何约束来扩展自定义视图?

@objc func bannerTapped(_ sender:UITapGestureRecognizer)
        self.bannerMessageLabel.numberOfLines = 0
        self.bannerMessageLabel.text = "Unicode characters take up multiple GSM characters. When a Unicode symbol appears in a text, it is usually segmented at the 70-character mark, thus making it even harder for the recipient to decipher the message."
        self.bannerMessageLabel.sizeToFit()
        self.bannerView.setNeedsLayout()
        self.bannerView.layoutIfNeeded()
    

附加图片。我对它们没有任何限制,因为我将它留在 Stack 视图上来处理它。

【问题讨论】:

您能分享添加到您的横幅视图中的约束吗 【参考方案1】:

我创建了一个示例 ViewController 来满足您的需求。我认为您缺少的是以下锚:

bannerView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor).isActive = true

示例视图控制器:

class TestViewController: UIViewController 

    // MARK: - UIObjects
    private let bannerView: UIView = 
        let bannerView = UIView()
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        bannerView.backgroundColor = .white
        return bannerView
    ()

    private let stackView: UIStackView = 
        let stackView = UIStackView()
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical // Works for both horizontal and vertical axis
        return stackView
    ()

    private let bannerWarningImage: UIImageView = 
        let imageView = UIImageView()
        imageView.image = UIImage(named: "arrow-right")
        imageView.backgroundColor = .orange
        return imageView
    ()
    private let bannerMessageLabel: UILabel = 
        let label = UILabel()
        label.text = "Unicode characters take up multiple GSM characters. When a Unicode symbol appears in a text"
        label.numberOfLines = 0
        label.backgroundColor = .green
        return label
    ()

    // MARK: - Overrides
    override func viewDidLoad() 
        super.viewDidLoad()

        bannerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(bannerTapped)))
        addUIElements()
        addConstrains()
    

    // MARK: - UISetup
    private func addUIElements() 
        view.backgroundColor = .purple
        view.addSubview(bannerView)
        bannerView.addSubview(stackView)

        stackView.addArrangedSubview(bannerWarningImage)
        stackView.addArrangedSubview(bannerMessageLabel)
    

    private func addConstrains() 
        bannerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        bannerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        bannerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        bannerView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor).isActive = true

        stackView.topAnchor.constraint(equalTo: bannerView.topAnchor).isActive = true
        // The -10 for you to see that the bannerView height is actually chaning
        stackView.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor, constant: -10).isActive = true
        stackView.leadingAnchor.constraint(equalTo: bannerView.leadingAnchor).isActive = true
        stackView.trailingAnchor.constraint(equalTo: bannerView.trailingAnchor).isActive = true
    

    // MARK: - Actions
    @objc private func bannerTapped() 
        self.bannerMessageLabel.text = "Unicode characters take up multiple GSM characters. When a Unicode symbol appears in a text, it is usually segmented at the 70-character mark, thus making it even harder for the recipient to decipher the message."
    

【讨论】:

以上是关于Swift - 在标签中动态调整大小后动态调整视图大小的主要内容,如果未能解决你的问题,请参考以下文章

UITableViewCell 不使用 Swift 动态调整大小

在 UITableView 页脚中动态设置和调整标签大小

Swift:动态 UIView 在调整大小后将 UIView 推到其下方

使用自动布局动态更改子视图后调整超级视图的大小

使用自动布局约束动态调整表格视图单元格的大小

使用动态大小的子视图调整 UIView 子类的大小