“HStack<Content> 初始值设定项的结果未使用/HStack 未出现在视图中

Result of 'HStack<Content>' initializer is unused / HStack Not Appearing in View

提问人:ERACED 提问时间:12/25/2020 最后编辑:pawello2222ERACED 更新时间:12/25/2020 访问量:662

问:

我正在尝试在我创建的视图中显示评级 (1-5) 的星号,但我收到此警告,并且星号没有出现在视图上。HStackResult 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
                }
            }
        }
    }
    
}

当前输出图像:

Output

有谁知道我该如何解决此警告并让我的星星正确出现在屏幕上?HStack

iOS Swift SwiftUI 警告 hstack

评论

1赞 pawello2222 12/25/2020
您只能在 SwiftUI 结构中使用 SwiftUI 代码。不能在 UIViewController 中使用它们。查看 developer.apple.com/tutorials/swiftui

答:

0赞 Harry Jin 12/25/2020 #1

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

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

stackView.axis = .Horizontal

请参考此 URL。https://developer.apple.com/documentation/uikit/uistackview

1赞 Asperi 12/25/2020 #2

我们可以将 SwiftUI 代码分离到独立视图中,然后使用 .UIHostingController

这是可能的解决方案的演示。使用 Xcode 12.1 / iOS 14.1 进行测试(当然您可以根据需要调整布局)

demo

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
                    }
            }
        }
    }
}