1

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

4

2 に答える 2

0

SwiftUI コードをスタンドアロン ビューに分離し、それを を使用してビュー コントローラーに統合できますUIHostingController

これは可能な解決策のデモです。Xcode 12.1 / iOS 14.1 でテスト済み (もちろん、必要に応じてレイアウトを調整できます)

デモ

class AddMeal: UIViewController {

    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
        let stars = UIHostingController(rootView: StarsView())
        self.view.addSubview(stars.view)
        self.addChild(stars)
        stars.view.backgroundColor = .clear

        stars.view.translatesAutoresizingMaskIntoConstraints = false
        stars.view.sizeToFit()
        stars.view.topAnchor.constraint(equalTo: mealRating.bottomAnchor, constant: 25).isActive = true
        stars.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    }
}

struct StarsView: View {
    @State var stars = -1
    var body: some View {
        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
                    }
            }
        }
    }
}
于 2020-12-25T03:15:20.027 に答える