- vừa được xem lúc

Make a placeholder content in SwiftUI with Redacted & Unredacted

0 0 2

Người đăng: Hieu Xuan Leu

Theo Viblo Asia

alt

import SwiftUI struct PlaceholderView: View { var body: some View { VStack(spacing: 20) { RoundedRectangle(cornerRadius: 10) .foregroundColor(.gray) .frame(height: 200) .redacted(reason: .placeholder) Text("Title") .font(.title) .redacted(reason: .placeholder) Text("Description") .font(.body) .multilineTextAlignment(.center) .redacted(reason: .placeholder) } .padding() }
} struct ContentView: View { @State var isLoading = true var body: some View { if isLoading { PlaceholderView() .redacted(reason: .placeholder) } else { VStack(spacing: 20) { Image(systemName: "person.crop.circle.fill") .resizable() .frame(width: 100, height: 100) Text("Hieu X. Leu") .font(.title) Text("Software Developer") .font(.body) .multilineTextAlignment(.center) } .padding() } Button(action: { isLoading.toggle() }, label: { Text(isLoading ? "Show content" : "Hide content") }) }
}

In this example, I creating a PlaceholderView that contains a gray rectangle as a placeholder for an image, a title, and a description. I using the .redacted(reason:) modifier on each of these views to redact the content and display a placeholder instead.

I also using a @State property called isLoading to toggle between the redacted and unredacted views. When isLoading is true, I show the PlaceholderView with the .redacted(reason:) modifier. When isLoading is false, I show the unredacted content.

To switch between the redacted and unredacted views, I using a Button with an action that toggles the isLoading state.

This is useful if you want to display a placeholder while data is being loaded, and then switch to the actual content once it’s available.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tạo hiệu ứng Story của Instagram bằng SwiftUI

. Có lẽ chúng ta đã quá quen thuộc với Story ở trên Instagram hoặc trên Facebook. Nó là những video và hình ảnh được liên kết với nhau như một thư viện hình ảnh để kể câu truyện nào đó.

0 0 39

- vừa được xem lúc

SwiftUI: Alert, Action Sheet

. A container for an alert presentation. . Trong hướng dẫn này, bạn sẽ tìm hiểu về alert trong SwiftUI:. .

0 0 23

- vừa được xem lúc

Passing methods as SwiftUI view actions

Thông thường khi ta xử lý các tương tác với các SwiftUI view, chúng ta thường sử dụng các closure để xác định các actions mà chúng ta muốn thực hiện khi các sự kiện khác nhau xảy ra. Ví dụ: AddItemView sau có hai thành phần tương tác là TextField và Button, cả hai đều cho phép người dùng thêm Item m

0 0 24

- vừa được xem lúc

[SwiftUI] Gradient

Với Gradient, bạn sẽ có thể tạo ra nhiều màu sắc hơn và đẹp hơn. . Apple Documentation. .

0 0 17

- vừa được xem lúc

Sử dụng TabView trong SwiftUI

I. Giới thiệu.

0 0 19

- vừa được xem lúc

Tạo checkboxes hiệu ứng đẹp mắt với SwiftUI

. 1. RoundedRectangle CheckBox. Với loại checkbox đầu tiên chúng tạo tạo một struct view và thêm RoundedRectangle. Sử dụng hàm stroke và thêm cho nó StrikeStyle với kích thước dòng kẻ mà bạn mong muốn.

0 0 22