SwiftUI 是 Apple 在 WWDC 2019 推出的新框架,它可以讓開發者更輕鬆地建立出美觀的 App。在 SwiftUI 中,開發者可以輕鬆地使用颜色和背景來增加 App 的美感。本文將介紹如何在 SwiftUI 中使用颜色和背景。

使用顏色

在 SwiftUI 中,可以使用 Color 類別來定義顏色。Color 類別接受一個 Color.Model 參數,可以指定使用的顏色模型,例如 .rgb.hsl.grayScale

例如,可以使用 .rgb 模型來定義一個紅色:

let red = Color(.rgb, red: 1, green: 0, blue: 0)

也可以使用 .hsl 模型來定義一個橙色:

let orange = Color(.hsl, hue: 0.08, saturation: 1, brightness: 0.5)

使用背景

在 SwiftUI 中,可以使用 background() 修飾器來設定背景顏色。例如,可以將一個文字框的背景設定為紅色:

Text("Hello World")
    .background(Color.red)

也可以將一個按鈕的背景設定為橙色:

Button(action: {
    // do something
}) {
    Text("Tap me")
}
.background(Color.orange)

總結

本文介紹了如何在 SwiftUI 中使用颜色和背景。可以使用 Color 類別來定義顏色,並使用 background() 修飾器來設定背景顏色。這樣可以讓開發者更輕鬆地建立出美觀的 App。

推薦閱讀文章

推薦閱讀文章

            <a href="https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-colors-and-backgrounds-in-swiftui">How to use colors and backgrounds in SwiftUI</a><br>
            <a href="https://www.appcoda.com/swiftui-colors/">How to Use Colors in SwiftUI</a><br>
            <a href="https://www.raywenderlich.com/724-swiftui-colors-and-borders">SwiftUI Colors and Borders</a><br>
            <a href="https://www.hackingwithswift.com/books/ios-swiftui/using-colors-in-swiftui">Using Colors in SwiftUI</a><br>
            <a href="https://www.appcoda.com/swiftui-background-color/">How to Set Background Color in SwiftUI</a

延伸閱讀本站文章

更多swift相關文章

延伸閱讀本站文章

SwiftUI 入門指南
Swift 圖片緩存 🎥高效快速存取圖片
Swift UIViewController 自定義返回按钮 🔙🎨
Swift UIGestureRecognizer使用🖐️ – 手勢辨識教學
Swift 離線資料存取 💾 離線資料快速存取法
Swift 影片拍攝:AVCaptureSession 🎥
Swift 圖片下載 🖼️
💾Swift 存儲 & 讀取數據 | 數據持久化 💾
Swift 動畫效果實作 (UIView.animate)
Swift 數字輪播器 🔢🔄

推薦學習youtube影片

【 SwiftUI 】#04 – Color / 顏色

在 SwiftUI 中使用颜色和背景

Categorized in:

Tagged in:

,