我有一个带有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,这样在显示选中的内容之前,之前的内容就会被清除,以及如何做到这一点?
EDIT: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))
}
你应该学会把相关的视图组合在一起。现在,你将所有的视图都添加为 view
. 这是很没有条理的,导致你现在的情况--不能有效地显示某些视图。
组织视图的一种方法是将 "按部门 "的视图添加到一个大的 UIView
和所有 "按配方 "的观点到另一个大的。UIView
.
var byDepartmentContainerView: UIView!
func setUpContentByDepartment() {
let recipeHeader = UILabel(frame: ...)
recipeHeader.setActivityDescription(label: "Coffee and Tea")
recipeHeader.textColor = UIColor(hexString: "#FC6A03")
recipeHeader.font = UIFont.systemFont(ofSize: 20)
let ingredient1Name = UILabel(frame: ...)
ingredient1Name.setActivityDescription(label: "Green tea leaves")
ingredient1Name.font = UIFont.systemFont(ofSize: 16)
let ingredient1Amount = UILabel(frame: ...)
ingredient1Amount.setActivityDescription(label: "1 tablespoons")
ingredient1Amount.font = UIFont.systemFont(ofSize: 12)
let ingredient2Name = UILabel(frame: ...)
ingredient2Name.setActivityDescription(label: "Coffee powder")
ingredient2Name.font = UIFont.systemFont(ofSize: 16)
let ingredient2Amount = UILabel(frame: ...)
ingredient2Amount.setActivityDescription(label: "3 tablespoons")
ingredient2Amount.font = UIFont.systemFont(ofSize: 12)
byDepartmentContainerView = UIView(frame: ...)
byDepartmentContainerView.addSubview(recipeHeader)
byDepartmentContainerView.addSubview(ingredient1Name)
byDepartmentContainerView.addSubview(ingredient1Amount)
byDepartmentContainerView.addSubview(ingredient2Name)
byDepartmentContainerView.addSubview(ingredient2Amount)
}
var byRecipeContainerView: UIView!
func setUpContentByRecipe() {
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)
byRecipeContainerView = UIView(frame: ...)
byRecipeContainerView.addSubview(recipeHeader)
byRecipeContainerView.addSubview(ingredient1Name)
byRecipeContainerView.addSubview(ingredient1Amount)
byRecipeContainerView.addSubview(ingredient2Name)
byRecipeContainerView.addSubview(ingredient2Amount)
}
注意,我省略了视图的框架。请给容器视图一个足够大的框架,以容纳所有的子视图,并调整子视图的框架,使它们是 相对于容器视图的坐标系.
另外,你似乎在两种方法中都画了同样的线,所以可以做一次就再也不做了。
func drawLines() {
view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
}
在 viewDidLoad
你可以调用这三个方法。
setUpContentByDepartment()
setUpContentByRecipe()
drawLines()
view.addSubview(byDepartmentContainerView)
view.addSubview(byRecipeContainerView)
byRecipeContainerView.isHidden = true
现在你可以做一些像这样的事情了
switch sender.selectedSegmentIndex {
case 0:
byRecipeContainerView.isHidden = true
byDepartmentContainerView.isHidden = false
case 1:
byRecipeContainerView.isHidden = false
byDepartmentContainerView.isHidden = true
default:
break
}
P. S. 硬编码视图的框架是一个非常糟糕的主意。它不能本地化,不能适应不同的设备,还有其他问题。请了解一下 自动排版约束.