You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中如何为Suggest视图设置圆角边缘?

为SwiftUI的Suggest视图设置圆角边缘的方法

给Suggest视图添加圆角有两种常用且简洁的方式,以下是具体实现:

方法一:使用.cornerRadius()修饰符

这是最直接的方式,适合所有圆角大小一致的场景,只需在视图的背景设置后添加该修饰符:

struct Suggest: View {
    var body: some View {
        HStack {
            Text("SUGGERIMENTI\n\nCon la funzione di entrata tramite NFC non hai bisogno di selezionare quale sessione avviare")
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding()
                .font(Font.custom("Medium", size: 18))
                .foregroundColor(Color.white)
        }
        .background(Color(red: 57 / 255, green: 63 / 255, blue: 147 / 255))
        .frame(width: UIScreen.screenWidth - 50, height: 200)
        .cornerRadius(16) // 自定义圆角大小,数值按需调整
    }
}

注意:.cornerRadius()必须放在.background()之后,否则背景会覆盖圆角效果。

方法二:用RoundedRectangle作为背景

这种方式支持自定义不同角的圆角大小,灵活性更高,还能配合添加阴影等额外效果:

struct Suggest: View {
    var body: some View {
        HStack {
            Text("SUGGERIMENTI\n\nCon la funzione di entrata tramite NFC non hai bisogno di selezionare quale sessione avviare")
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding()
                .font(Font.custom("Medium", size: 18))
                .foregroundColor(Color.white)
        }
        .background(
            // 统一圆角
            RoundedRectangle(cornerRadius: 16)
                .fill(Color(red: 57 / 255, green: 63 / 255, blue: 147 / 255))
            // 若需自定义特定角的圆角,替换为下面的代码:
            // RoundedRectangle(cornerRadii: RectangleCornerRadii(
            //     topLeft: 16,
            //     topRight: 16,
            //     bottomLeft: 0,
            //     bottomRight: 0
            // ))
            // .fill(Color(red: 57 / 255, green: 63 / 255, blue: 147 / 255))
        )
        .frame(width: UIScreen.screenWidth - 50, height: 200)
    }
}

额外提示

你的Sedi视图结构与Suggest完全一致,同样可以使用上述两种方法添加圆角效果。

内容的提问来源于stack exchange,提问作者riki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:30:41