作成したビューに評価 (1 ~ 5) の星を表示しようとしてHStack
いますが、この警告が表示さ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
この警告に対処して星を画面に正しく表示する方法を知っている人はいますか?