简体   繁体   中英

How to achieve ios blur background with roundedrectangle()?

I have this code:

import SwiftUI
 struct MyBackGround: View {
    var body: some View {
        ZStack() {
            Color.pink.ignoresSafeArea()
            RoundedRectangle(cornerRadius: 20).background(Color.white.opacity(0.5))
                .frame(width: 220, height: 100)
        }
    }
}

And this image: 模糊图像背景

I want to achieve the similar look of iOS blur background, but it did not work.

You can achieve this with a simpler code. Your code does not work because your RoundedRectangle contains black as its foreground color originally.

import SwiftUI

struct ContentView: View {

var body: some View {
    ZStack {
        Color.pink.ignoresSafeArea(.all)
        RoundedRectangle(cornerRadius: 20)
            .fill(.ultraThinMaterial)
            .frame(width: 200, height: 200)
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM