我對 SwiftUI 很陌生。也許我不完全理解 @State 注釋變數是如何作業的。據我所知,當你更新它們的值時,如果它們在同一個視圖中,螢屏也會更新(在這種情況下是螢屏的背景顏色)。我想我完全按照視頻教程(SwiftUI 基礎教程)進行了操作,但我的代碼仍然無法正常作業。順便說一句,如果值得一提的話,我使用的是 Xcode 版本 13.2.1。在我ContentView.swift這就是我所擁有的:
//
// ContentView.swift
//
import SwiftUI
struct ContentView: View {
@State private var isNight = false //Variable to be changed when pressing the button
var body: some View {
ZStack {
//The BackgroundView color should change when pressing the button
BackgroundView(topColor: isNight ? .black : .blue,
bottomColor: isNight ? .gray : Color("lightBlue"))
VStack{
CityTextView(cityName: "Cupertino, CA")
MainWeatherStatusView(imageName: "cloud.sun.rain.fill",
temperature: 72)
HStack(spacing: 0){
WeatherView(dayOfWeek: "TUE",
imageName: "cloud.sun.fill",
temperature: 74)
WeatherView(dayOfWeek: "WED",
imageName: "sun.max.fill",
temperature: 30)
WeatherView(dayOfWeek: "THU",
imageName: "wind.snow",
temperature: 5)
WeatherView(dayOfWeek: "FRI",
imageName: "sunset.fill",
temperature: 60)
WeatherView(dayOfWeek: "SAT",
imageName: "snow",
temperature: 74)
}
Button{
isNight.toggle() //Changing this should update my View, right?
} label: {
WeatherButton(title: "Change Day Time",
textColor: .blue,
backgroundColor: .white)
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct WeatherView: View {
var dayOfWeek: String
var imageName: String
var temperature: Int
var body: some View {
VStack{
Text(dayOfWeek)
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.padding()
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 40, height: 40)
Text("\(temperature)°")
.font(.system(size: 30, weight: .medium))
.foregroundColor(.white)
Spacer()
}
}
}
struct BackgroundView: View {
var topColor: Color
var bottomColor: Color
var body: some View {
LinearGradient(gradient: Gradient(colors: [.blue, Color("lightBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
.edgesIgnoringSafeArea(.all)
}
}
struct CityTextView: View {
var cityName: String
var body: some View {
Text(cityName)
.font(.system(size: 32, weight: .medium, design: .default))
.foregroundColor(.white)
.padding()
}
}
struct MainWeatherStatusView: View {
var imageName: String
var temperature: Int
var body: some View {
VStack(spacing: 8){
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 180, height: 180)
Text("\(temperature)°")
.font(.system(size: 70, weight: .medium))
.foregroundColor(.white)
}
}
}
如果您想復制粘貼代碼并想擺脫圖示,這里是沒有它們的代碼:
//
// ContentView.swift
//
import SwiftUI
struct ContentView: View {
@State private var isNight = false //VARIABLE TO BE CHANGED WHEN PRESSING BUTTON
var body: some View {
ZStack {
//The BackgroundView color should change when pressing the button
BackgroundView(topColor: isNight ? .black : .blue,
bottomColor: isNight ? .gray : Color("lightBlue"))
VStack{
CityTextView(cityName: "Cupertino, CA")
Button{
isNight.toggle() //CHANGING THIS SHOULD UPDATE MY VIEW, RIGHT?
} label: {
WeatherButton(title: "Change Day Time",
textColor: .blue,
backgroundColor: .white)
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct WeatherView: View {
var dayOfWeek: String
var imageName: String
var temperature: Int
var body: some View {
VStack{
Text(dayOfWeek)
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.padding()
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 40, height: 40)
Text("\(temperature)°")
.font(.system(size: 30, weight: .medium))
.foregroundColor(.white)
Spacer()
}
}
}
struct BackgroundView: View {
var topColor: Color
var bottomColor: Color
var body: some View {
LinearGradient(gradient: Gradient(colors: [.blue, Color("lightBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
.edgesIgnoringSafeArea(.all)
}
}
struct CityTextView: View {
var cityName: String
var body: some View {
Text(cityName)
.font(.system(size: 32, weight: .medium, design: .default))
.foregroundColor(.white)
.padding()
}
}
struct MainWeatherStatusView: View {
var imageName: String
var temperature: Int
var body: some View {
VStack(spacing: 8){
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 180, height: 180)
Text("\(temperature)°")
.font(.system(size: 70, weight: .medium))
.foregroundColor(.white)
}
}
}
uj5u.com熱心網友回復:
在您當前的BackgroundView代碼中,雖然您有topColor和bottomColor引數,但您實際上并沒有在視圖中使用它們。您可能希望將它們包含在漸變中:
struct BackgroundView: View {
var topColor: Color
var bottomColor: Color
var body: some View {
LinearGradient(gradient: Gradient(colors: [topColor, bottomColor]), startPoint: .topLeading, endPoint: .bottomTrailing)
.edgesIgnoringSafeArea(.all)
}
}
此外,請確保您Color("lightBlue")的資產目錄中有自定義顏色(如 )。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429147.html
