我正在寻找删除最后一个uiview的方法。请帮忙。
这是我的代码
import UIKit
class TabBarCtr: UITabBarController{
var sidebarView: SidebarView!
var blackScreen: UIView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.backgroundColor = .white
// 这里有东西
sidebarView = SidebarView(frame: CGRect(x: 0, y: 0, width: 0, height: self.view.frame.height))
sidebarView.delegate = self
sidebarView.layer.zPosition = 100
self.view.isUserInteractionEnabled = true
view.addSubview(sidebarView)
blackScreen=UIView(frame: self.view.bounds)
blackScreen.backgroundColor=UIColor(white: 0, alpha: 0.5)
blackScreen.isHidden = true
view.addSubview(blackScreen)
blackScreen.layer.zPosition=99
let tapGestRecognizer = UITapGestureRecognizer(target: self, action: #selector(blackScreenTapAction(sender:)))
blackScreen.addGestureRecognizer(tapGestRecognizer)
}
override func viewDidLoad() {
super.viewDidLoad()
viewControllers = [
CreateDumyNavControllerWithTitle(title: "Home", imageName: "bell")
]
}
private func CreateDumyNavControllerWithTitle(title:String, imageName:String, collection:Bool = false)-> UINavigationController {
let viewCtr = HomeCtrViewController()
viewCtr.navigationItem.leftBarButtonItem = setupLeftNavBarButtons()
let navCtr = UINavigationController(rootViewController: viewCtr)
navCtr.hidesBarsOnSwipe = true
navCtr.tabBarItem.title = title
navCtr.tabBarItem.image = UIImage(named: imageName)
return navCtr
}
func setupLeftNavBarButtons()-> UIBarButtonItem{
let settingsImage = UIImage(named: "menu-2")?.withRenderingMode(.automatic)
let settingBarButtonItem = UIBarButtonItem(image: settingsImage, style: .plain, target: self, action: #selector(btnMenuAction))
return settingBarButtonItem
}
@objc func btnMenuAction() {
blackScreen.isHidden = false
UIView.animate(withDuration: 0.3, animations: {
self.sidebarView.frame=CGRect(x: 0, y: 0, width: 250, height: self.sidebarView.frame.height)
}) { (complete) in
self.blackScreen.frame=CGRect(x: self.sidebarView.frame.width, y: 0, width: self.view.frame.width-self.sidebarView.frame.width, height: self.view.bounds.height+100)
}
}
@objc func blackScreenTapAction(sender: UITapGestureRecognizer) {
blackScreen.isHidden=true
blackScreen.frame=self.view.bounds
UIView.animate(withDuration: 0.3) {
self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
}
}
}
// sideMenu
class SidebarView: UIView, UITableViewDelegate, UITableViewDataSource {
var titleArr = [String]()
weak var delegate: SidebarViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor=UIColor(red: 54/255, green: 55/255, blue: 56/255, alpha: 1.0)
self.clipsToBounds=true
titleArr = ["Tam Tuyet", "test", "test1"]
setupViews()
myTableView.delegate=self
myTableView.dataSource=self
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
myTableView.tableFooterView=UIView()
myTableView.separatorStyle = UITableViewCellSeparatorStyle.none
myTableView.allowsSelection = true
myTableView.bounces=false
myTableView.showsVerticalScrollIndicator=false
myTableView.backgroundColor = UIColor.clear
}
}
// 测试启动器
class TestLauncher: NSObject{
let frame = UIApplication.shared.keyWindow?.frame
let testContainer: UIView = {
let view = UIView()
view.backgroundColor = UIColor.brown
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.masksToBounds = true
view.layer.zPosition = 1
return view
}()
let btnClose: UIButton = {
let origImage = UIImage(named: "cancel")
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
let btnClose = UIButton()
btnClose.setImage(tintedImage, for: .normal)
btnClose.tintColor = .white
btnClose.addTarget(self, action:#selector(handleDismiss), for: UIControlEvents.touchUpInside)
btnClose.frame = CGRect(x: 10, y: 30, width: 16, height: 16)
return btnClose
}()
func show() {
testContainer.addSubview(btnClose)
if let window = UIApplication.shared.keyWindow {
window.addSubview(testContainer)
setUpTestContainer()
UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping: 1, initialSpringVelocity:1, options: .curveEaseOut, animations: {
self.testContainer.frame = CGRect(x: 0, y: 0, width: (self.frame?.width)!, height: (self.frame?.height)!)
}, completion: nil)
}
}
func setUpTestContainer(){
testContainer.frame = CGRect(x: 0, y: (self.frame?.height)!, width: (self.frame?.width)!, height: (self.frame?.height)!)
}
@objc func handleDismiss() {
UIView.animate(withDuration: 0.2, animations: {
if let window = UIApplication.shared.keyWindow{
self.testContainer.frame = CGRect(x: 0, y: window.frame.height, width:
self.testContainer.frame.width, height: self.testContainer.frame.height)
}
}, completion: {(finished:Bool) in
self.testContainer.removeFromSuperview()
})
}
override init() {
super.init()
}
}
我怎样才能删除最后一个uiview?我是新手。请帮我。感谢先进