Open11

備忘録

平成n年生まれ@iOSエンジニア志望平成n年生まれ@iOSエンジニア志望

overlay & background


Circleは高さを持たない点に注意する。

Text("Sample")
    .overlay {
        Circle()
            .foregroundStyle(Color(uiColor: .systemGray6))
            .scaledToFill()
    }
Divider()
    .padding(.vertical)
Text("Sample")
    .background {
        Circle()
            .foregroundStyle(Color(uiColor: .systemGray6))
            .scaledToFill()
    }
平成n年生まれ@iOSエンジニア志望平成n年生まれ@iOSエンジニア志望

よく使うボタンの装飾

paddingが二つあるのは少々気持ち悪いが、サイズに合わせ自動的に調整してくれる。RoundedRectangleをclipShapeの引数に持たせたいが、CGFloatかCGSizeを指定する必要があるので、Capsuleで妥協。colorInvertを用いることでダークモードにも対応できる。

Text("Sample")
    .foregroundStyle(.primary)
    .colorInvert()
    .font(.title)
    .fontWeight(.semibold)
    .padding()
    .padding(.horizontal)
    .background(.blue)
    .clipShape(Capsule())
平成n年生まれ@iOSエンジニア志望平成n年生まれ@iOSエンジニア志望

ContentUnavailableView

https://developer.apple.com/documentation/swiftui/contentunavailableview

struct ContentView: View {
    var body: some View {
        ContentUnavailableView("該当者なし", systemImage: "person.slash", description: Text("条件を変更し再度お試しください。"))

        ContentUnavailableView.search

        ContentUnavailableView.search(text: "apple")

        ContentUnavailableView(label: {
            Label("No Mail", systemImage: "tray.fill")
        }, description: {
            Text("New mails you receive will appear here.")
        }, actions: {
            Button(action: {}) {
                Text("Refresh")
            }
        })
    }
}