I have a stack view to which I add some buttons when the UI loads. I have made these work quite well as they are, but I want to simplify my interface. Instead of showing a button for each feed, I want to add a horizontal stack view showing a label and a stepper to alter the values for each feed type.
This is what I have now:
My plan is to change the addButtons function so that instead of adding buttons, it will add a horizontal stack view containing a label and a stepper for each feed above 0 in the inventory. This would make my interface much cleaner. Not sure if this is even possible, maybe there are better ways of doing it. Any tips would be really helpful!
This is what I have now:
Code Block struct FeedProduct { var name : String var code : String } var feedProducts = [ FeedProduct(name: "Oats", code: "oats"), FeedProduct(name: "Straw", code: "straw"), FeedProduct(name: "Salt", code: "salt") ] var feedInventory: [ String : Int ] = [ "straw" : 10, "oats" : 20, "salt" : 0 ] func addButtons() { for (key,value) in feedInventory{ let button = UIButton() button.addTarget(self, action: #selector(self.selectFeed(sender:)), for: .touchUpInside) let filteredProducts = feedProducts.filter { $0.code.contains(key)} for feedProduct in filteredProducts { if value > 0 { button.setTitle(" \(feedProduct.name) , amount: \(value)", for: UIControl.State.normal) button.accessibilityIdentifier = feedProduct.code feedStack.addArrangedSubview(button) }}}} @objc func selectFeed(sender: UIButton){ //this is where assign the selected feed type, and I had an external stepper button to alter the amount of feed }
My plan is to change the addButtons function so that instead of adding buttons, it will add a horizontal stack view containing a label and a stepper for each feed above 0 in the inventory. This would make my interface much cleaner. Not sure if this is even possible, maybe there are better ways of doing it. Any tips would be really helpful!
Why don't you do it as you describe?it will add a horizontal stack view containing a label and a stepper
Code Block func addButtons() { for (key,value) in feedInventory where value > 0 { let filteredProducts = feedProducts.filter { $0.code.contains(key)} for feedProduct in filteredProducts { let horizontalStack = UIStackView() horizontalStack.axis = .horizontal horizontalStack.alignment = .firstBaseline let label = UILabel(frame: .zero) label.text = " \(feedProduct.name) , amount: \(value)" //... label.widthAnchor.constraint(equalToConstant: 200).isActive = true horizontalStack.addArrangedSubview(label) let stepper = UIStepper(frame: .zero) //... stepper.accessibilityLabel = "\(feedProduct.code) up/down" horizontalStack.addArrangedSubview(stepper) feedStack.addArrangedSubview(horizontalStack) } } }