更新 UIStackView 中标签的文本并同时保持文本居中?

发布于 2025-01-20 04:41:52 字数 546 浏览 1 评论 0原文

当我在堆栈视图中放置带有居中文本的标签,然后更新文本时,它不再显示居中。

import PlaygroundSupport
import UIKit

let stackView = UIStackView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 20)))

let label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 20)))
label.text = "Hello"
label.textAlignment = .center

stackView.addArrangedSubview(label)

label.text = "World"

PlaygroundPage.current.liveView = stackView

预期的结果是以“世界”为中心。然而,它看起来是左对齐的。调用layoutSubviews()没有帮助。

When I place a label with centered text in a stack view, then update the text, it no longer appears centered.

import PlaygroundSupport
import UIKit

let stackView = UIStackView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 20)))

let label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 20)))
label.text = "Hello"
label.textAlignment = .center

stackView.addArrangedSubview(label)

label.text = "World"

PlaygroundPage.current.liveView = stackView

The expected outcome is that "World" is centered. However, it appears leftaligned. Calling layoutSubviews() did not help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2025-01-27 04:41:52

如果没有自动布局,堆栈视图就不能很好地发挥作用 - 特别是在 Playgrounds 中。

试试这样:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()

        view.backgroundColor = .white

        let stackView = UIStackView()
        
        let label = UILabel()
        label.text = "Hello"
        label.textAlignment = .center

        // so we can see the frame
        label.backgroundColor = .green
        
        stackView.addArrangedSubview(label)
        
        label.text = "World"

        view.addSubview(stackView)
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            stackView.widthAnchor.constraint(equalToConstant: 100.0),
            stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0),
        ])
        
        self.view = view
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

Stack views don't play well without auto-layout - particularly in Playgrounds.

Try it like this:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()

        view.backgroundColor = .white

        let stackView = UIStackView()
        
        let label = UILabel()
        label.text = "Hello"
        label.textAlignment = .center

        // so we can see the frame
        label.backgroundColor = .green
        
        stackView.addArrangedSubview(label)
        
        label.text = "World"

        view.addSubview(stackView)
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            stackView.widthAnchor.constraint(equalToConstant: 100.0),
            stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0),
        ])
        
        self.view = view
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文