迅速如何调整文本的背景区域

发布于 2025-02-01 21:37:00 字数 952 浏览 3 评论 0原文

我有一个带有一些堆栈的Swift文件,其中上文的作用很奇怪。我不明白为什么“控制器”文本的背景颜色延伸到屏幕的末端。如何调整背景的高度?

 var body: some View {
        NavigationView {
            ZStack {
                Color("Themecolor")
                .edgesIgnoringSafeArea(.all)
                VStack {
                    HStack(spacing: 0) {
                      Text("BIKE")
                        .font(.system(size: 52))
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                      Text("Controller")
                        .font(.system(size: 52))
                        .foregroundColor(.black)
                        .background(
                            .white)
                    }
                    .offset(y: -50)
                    

I have a swift file with a few stacks, of which the upper text acts weirdly. I can't understand why the background color of the "controller"-text extends up to the end of the screen. How can I adjust the height of the background?

 var body: some View {
        NavigationView {
            ZStack {
                Color("Themecolor")
                .edgesIgnoringSafeArea(.all)
                VStack {
                    HStack(spacing: 0) {
                      Text("BIKE")
                        .font(.system(size: 52))
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                      Text("Controller")
                        .font(.system(size: 52))
                        .foregroundColor(.black)
                        .background(
                            .white)
                    }
                    .offset(y: -50)
                    

enter image description here

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

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

发布评论

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

评论(1

以歌曲疗慰 2025-02-08 21:37:00

默认情况下,使用的安全区域应用了带有样式的新背景修饰符,因此我们可以明确将其关闭,例如

  Text("Controller")
    .font(.system(size: 52))
    .foregroundColor(.black)
    .background(
        .white, ignoresSafeAreaEdges: .bottom)  // << here !!

“

用Xcode 13.4/ios 15.5进行

测试是使用不同的修饰符变体

  Text("Controller")
    .font(.system(size: 52))
    .foregroundColor(.black)
    .background(
        Rectangle().fill(.white))    // << here !!

By default new background modifier with style is applied with ignoring safe area for .all, so we can explicitly turn it off, like

  Text("Controller")
    .font(.system(size: 52))
    .foregroundColor(.black)
    .background(
        .white, ignoresSafeAreaEdges: .bottom)  // << here !!

demo

Tested with Xcode 13.4 / iOS 15.5

Alternate: for backward compatibility is to use different variant of modifier

  Text("Controller")
    .font(.system(size: 52))
    .foregroundColor(.black)
    .background(
        Rectangle().fill(.white))    // << here !!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文