【发布时间】:2020-05-19 16:20:39
【问题描述】:
我有一个带有 UISegmentedControl 的 viewController 可以在 2 个内容之间切换(按部门或按配方)。当我切换 segmentedControl(它将执行 changeColor 函数)时,我想更改与选择的段相对应的内容。每个段中的内容是几个 UILabel 然后像 view.addSubview(label) 一样添加它们。但是,我当前的代码所做的是将它们相互重叠。
class GroceryViewController: UIViewController {
var customSC = UISegmentedControl()
override func viewDidLoad() {
super.viewDidLoad()
setupSegmentedControl()
contentByDepartment()
}
func setupSegmentedControl() {
let items = ["BY DEPARTMENT", "BY RECIPE"]
customSC = UISegmentedControl(items: items)
customSC.selectedSegmentIndex = 0
let frame = UIScreen.main.bounds
customSC.frame = CGRect(x:frame.minX + 15, y:frame.minY + 100,
width:frame.width - 30, height:frame.height * 0.04)
customSC.layer.cornerRadius = 20
customSC.backgroundColor = UIColor(hexString: "#F7F7F7")
customSC.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hexString: "#FC6A03")], for: UIControl.State.selected)
customSC.addTarget(self, action: #selector(changeColor), for: .valueChanged)
view.addSubview(customSC)
}
@objc func changeColor(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
contentByDepartment() // contents are several UILabel adding to view as SubView
case 1:
contentByRecipe() // contents are several UILabel adding to view as SubView
default:
break
}
}
}
我已经搜索了解决方案,大多数是“UITableView 中的 reloadData()”,我没有。所以我不知道如何在不打扰 UIView 或 UITableView 的情况下重新加载这个 viewController。
问题是:是否可以单独从某个函数重新加载viewController,以便在显示所选内容之前清除以前的内容以及如何做到这一点?
编辑:contentByDepartment 和 contentByRecipe 的代码
func contentByDepartment() {
let recipeHeader = UILabel(frame: CGRect(x:50, y:180, width: 200, height: 20))
recipeHeader.setActivityDescription(label: "Coffee and Tea")
recipeHeader.textColor = UIColor(hexString: "#FC6A03")
recipeHeader.font = UIFont.systemFont(ofSize: 20)
let ingredient1Name = UILabel(frame: CGRect(x:70,y:210,width:200,height:20))
ingredient1Name.setActivityDescription(label: "Green tea leaves")
ingredient1Name.font = UIFont.systemFont(ofSize: 16)
let ingredient1Amount = UILabel(frame: CGRect(x:72,y:230,width:200,height:20))
ingredient1Amount.setActivityDescription(label: "1 tablespoons")
ingredient1Amount.font = UIFont.systemFont(ofSize: 12)
let ingredient2Name = UILabel(frame: CGRect(x:70,y:260,width:200,height:20))
ingredient2Name.setActivityDescription(label: "Coffee powder")
ingredient2Name.font = UIFont.systemFont(ofSize: 16)
let ingredient2Amount = UILabel(frame: CGRect(x:72,y:280,width:200,height:20))
ingredient2Amount.setActivityDescription(label: "3 tablespoons")
ingredient2Amount.font = UIFont.systemFont(ofSize: 12)
view.addSubview(recipeHeader)
view.addSubview(ingredient1Name)
view.addSubview(ingredient1Amount)
view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
view.addSubview(ingredient2Name)
view.addSubview(ingredient2Amount)
view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
}
func contentByRecipe() {
let recipeHeader = UILabel(frame: CGRect(x:50, y:180, width: 200, height: 20))
recipeHeader.setActivityDescription(label: "Cha-yen")
recipeHeader.textColor = UIColor(hexString: "#FC6A03")
recipeHeader.font = UIFont.systemFont(ofSize: 20)
let ingredient1Name = UILabel(frame: CGRect(x:70,y:210,width:200,height:20))
ingredient1Name.setActivityDescription(label: "Vanilla extract")
ingredient1Name.font = UIFont.systemFont(ofSize: 16)
let ingredient1Amount = UILabel(frame: CGRect(x:72,y:230,width:200,height:20))
ingredient1Amount.setActivityDescription(label: "1 tablespoons")
ingredient1Amount.font = UIFont.systemFont(ofSize: 12)
let ingredient2Name = UILabel(frame: CGRect(x:70,y:260,width:200,height:20))
ingredient2Name.setActivityDescription(label: "Sugar")
ingredient2Name.font = UIFont.systemFont(ofSize: 16)
let ingredient2Amount = UILabel(frame: CGRect(x:72,y:280,width:200,height:20))
ingredient2Amount.setActivityDescription(label: "3 tablespoons")
ingredient2Amount.font = UIFont.systemFont(ofSize: 12)
view.addSubview(recipeHeader)
view.addSubview(ingredient1Name)
view.addSubview(ingredient1Amount)
view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
view.addSubview(ingredient2Name)
view.addSubview(ingredient2Amount)
view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
}
【问题讨论】:
-
您当前的代码似乎可以执行您想要的操作...您当前的代码是做什么的?
-
它确实改变了内容,但它们会相互重叠。我希望做的是重新加载它,以便首先清除以前的内容,然后显示所选内容
-
然后先清除前一个。你必须自己做。没有人会为您做...您可以使用循环遍历要删除的视图的父视图的
subviews,然后调用removeFromSuperView。或者,将部门和配方内容视图添加为不同视图的子视图。这样,当分段控件发生变化时,您可以只显示/隐藏相应的超级视图。 -
如果您显示
contentByDepartment和contentByRecipe,我可能可以发布答案。 -
我已经编辑了帖子以显示这两个功能。
标签: swift uiviewcontroller uisegmentedcontrol