1

我正在尝试让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的星星正确出现在屏幕上?

4

2 回答 2

0

您必须在 UIKit 模式中使用 UIStackView 而不是 HStack(SwiftUI)。

UIStackView 具有指示水平或垂直的轴。

stackView.axis = .Horizontal

请参考这个网址。 https://developer.apple.com/documentation/uikit/uistackview

于 2020-12-24T22:14:00.160 回答
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 回答