我正在尝试让HStack
评级(1-5)的星星出现在我创建的视图中,但我收到此警告Result of 'HStack<Content>' initializer is unused
并且星星没有出现在视图上。
AddMeal
查看代码:
import SwiftUI
class AddMeal: UIViewController {
@State var stars = -1
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
// Add Meal Title Label
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
label.center = self.view.center
label.center.x = self.view.center.x
label.center.y = 75
label.font = label.font.withSize(30);
label.textAlignment = .center
label.textColor = .white
label.text = "Add Meal"
self.view.addSubview(label)
// Meal Title Label
let food = UILabel(frame: CGRect(x: 22, y: 100, width: 200, height: 50))
food.font = label.font.withSize(20);
food.textColor = .white
food.text = "Meal Title"
self.view.addSubview(food)
// Food Title Input Text Field
let sampleTextField = UITextField(frame: CGRect(x: 20, y: 150, width: 335, height: 40))
sampleTextField.placeholder = "Enter the Name of the Food or Drink..."
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextField.BorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextField.ViewMode.whileEditing
sampleTextField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
self.view.addSubview(sampleTextField)
// Meal Calories Label
let mealCalories = UILabel(frame: CGRect(x: 22, y: 200, width: 200, height: 50))
mealCalories.font = label.font.withSize(20);
mealCalories.textColor = .white
mealCalories.text = "Meal Calories"
self.view.addSubview(mealCalories)
// Calories Input Text Field
let sampleTextField2 = UITextField(frame: CGRect(x: 20, y: 250, width: 335, height: 40))
sampleTextField2.placeholder = "Enter the # of Calories..."
sampleTextField2.font = UIFont.systemFont(ofSize: 15)
sampleTextField2.borderStyle = UITextField.BorderStyle.roundedRect
sampleTextField2.autocorrectionType = UITextAutocorrectionType.no
sampleTextField2.keyboardType = UIKeyboardType.default
sampleTextField2.returnKeyType = UIReturnKeyType.done
sampleTextField2.clearButtonMode = UITextField.ViewMode.whileEditing
sampleTextField2.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
self.view.addSubview(sampleTextField2)
// Meal Rating Label
let mealRating = UILabel(frame: CGRect(x: 22, y: 300, width: 200, height: 50))
mealRating.font = label.font.withSize(20);
mealRating.textColor = .white
mealRating.text = "Meal Rating"
self.view.addSubview(mealRating)
// Rating Stars
HStack {
ForEach(0..<5){ i in
Image(systemName: "star.fill").resizable().frame(width: 30, height:30).foregroundColor(self.stars >= i ? .yellow : .gray).onTapGesture {
self.stars = i
}
}
}
}
}
当前输出图像:
有谁知道我如何解决这个警告并让我HStack
的星星正确出现在屏幕上?