我想做的很简单。让我解释一下我首先要了解的内容,在mainviewcontroller中,我在开始的导航栏,内容和bottombtn处有3个主视图。我有一个边栏btn,然后在mainviewcontroller中触发一个函数,每个函数都假定要删除中间视图(内容),然后放置一个新的子视图来取代内容。
问题:首先工作的功能是删除contentTxt并将其替换为新视图示例之一:connectView,但是问题是当我尝试触发另一个函数时,请说addAchievementView()它不会删除connectView,然后添加成就视图,而是让它在连接视图下方添加成就视图
我的目标:该函数用于删除上一个视图(占用内容空间的任何一个视图),然后添加一个新视图。有办法解决这个问题吗?还是有更好的方法来做到这一点?
这是mainViewController代码:
class mainViewController: UIViewController {
let navbar:navbarView = {
let content = navbarView()
return content
}()
let contentTxt:UITextView = {
let content = UITextView()
content.backgroundColor = UIColor.green
content.font = UIFont(name: "copperplate", size: 20)
return content
}()
let bottomBtn:UIButton = {
let content = UIButton()
content.backgroundColor = UIColor.blue
return content
}()
override func viewDidLoad() {
super.viewDidLoad()
navbar.translatesAutoresizingMaskIntoConstraints = false
contentTxt.translatesAutoresizingMaskIntoConstraints = false
bottomBtn.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(navbar)
view.addSubview(contentTxt)
view.addSubview(bottomBtn)
navbar.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
navbar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
navbar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
navbar.heightAnchor.constraint(equalToConstant: 50).isActive = true
contentTxt.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
contentTxt.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
contentTxt.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
contentTxt.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
bottomBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-10).isActive = true
bottomBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
bottomBtn.widthAnchor.constraint(equalToConstant: 70).isActive = true
bottomBtn.heightAnchor.constraint(equalToConstant: 70).isActive = true
}
@objc func addConnectView(){
print("addConnectView")
self.contentTxt.removeFromSuperview()
let connect:connectView = {
let content = connectView()
content.backgroundColor = UIColor.red
return content
}()
connect.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(connect)
self.view.sendSubview(toBack: connect)
connect.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
connect.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
connect.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
connect.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addTemplateView(){
print("addTemplateView")
self.contentTxt.removeFromSuperview()
let content:templateView = {
let content = templateView()
content.backgroundColor = UIColor.blue
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addSettingView(){
print("addSettingView")
self.contentTxt.removeFromSuperview()
let content:settingView = {
let content = settingView()
content.backgroundColor = UIColor.green
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addAchievementView(){
print("addAchievementView")
self.contentTxt.removeFromSuperview()
let content:achievementView = {
let content = achievementView()
content.backgroundColor = UIColor.gray
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addCommandView(){
print("addCommandView")
self.contentTxt.removeFromSuperview()
let content:commandView = {
let content = commandView()
content.backgroundColor = UIColor.cyan
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addListView(){
print("addListView")
self.contentTxt.removeFromSuperview()
let content:commandView = {
let content = commandView()
content.backgroundColor = UIColor.red
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func addNavbarView(){
print("addNavbarView")
self.contentTxt.removeFromSuperview()
let content:navbarListView = {
let content = navbarListView()
content.backgroundColor = UIColor.red
return content
}()
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
self.view.sendSubview(toBack: content)
content.topAnchor.constraint(equalTo: navbar.bottomAnchor, constant: 5).isActive = true
content.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
content.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func sideController(){
let next = self.storyboard?.instantiateViewController(withIdentifier: "sideViewController") as! sideViewController
self.present(next, animated: true, completion: nil)
}
@objc func profileController(){
let next = self.storyboard?.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController
self.present(next, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
在添加任何视图之前,请确保删除所有其他视图,因为您不知道当前显示的内容
self.contentTxt.removeFromSuperview()
self.content.removeFromSuperview() //
self.content.removeFromSuperview() // for settings
这行也是问题
self.view.sendSubview(toBack: content)
默认情况下,添加的视图位于parentView的所有子视图上方
我投票赞成创建placeHolderView
并在添加任何内容之前将其子视图删除,这可以防止您知道当前显示的是哪个子视图,也可以为添加的视图添加标签,并在添加任何新内容之前进行操作< / p>
self.view.subviews.forEach { if $0.tag == 11 {
$0.removeFromSuperview()
}}