将屏幕外的 SwiftUI 视图居中 [英] Center an off screen SwiftUI view

查看:19
本文介绍了将屏幕外的 SwiftUI 视图居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个水平居中圆的屏幕,该圆大于实际屏幕的宽度.不幸的是,我不知道如何水平居中将其固定到顶部.有没有一种简单的方法可以使用 SwiftUI 实现这样的结果?

I am trying to create a screen with a horizontally centered circle that is bigger than the actual screen's width. Unfortunately, I do not know how to center it horizontally and pin it to the top. Is there an easy way to achieve such results with SwiftUI?

struct DemoView: View {
    private let circleBackgroundPercentage: CGFloat = 1.3
    
    var body: some View {
        ZStack {
            GeometryReader { geometry in
                VStack(alignment: .center) {
                    Circle()
                        .fill(Color.yellow)
                        .frame(width: geometry.size.width * circleBackgroundPercentage, height: geometry.size.height * circleBackgroundPercentage, alignment: .center)
                }
            }
            Text("Demo")
        }.edgesIgnoringSafeArea(.all)
    }
}

]

推荐答案

这里是解决此类任务的可能方法.

Here is possible approach to solve such kind to task.

已准备好 Xcode 12.4/iOS 14.4

Prepared with Xcode 12.4/iOS 14.4

var body: some View {
    ZStack {
        VStack {
            Color.yellow
                .clipShape(Circle())
            Color.clear
        }
        .scaleEffect(1.8)   // << tune factor as needed !!
        Text("Demo")
    }.edgesIgnoringSafeArea(.all)
}

这篇关于将屏幕外的 SwiftUI 视图居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆