-3

My app look like this enter image description here

Then I click on menu it look like this, then click test enter image description here

Then click on button Close enter image description here

I'm looking for way to remove last uiview. Please help.

here is my code

import UIKit

class TabBarCtr: UITabBarController{

    var sidebarView: SidebarView!
    var blackScreen: UIView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        view.backgroundColor = .white

// something here

        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
}



}

// testLauncher

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()

}
}

how can I remove last uiview? I'm newbie. Please help me. Thank in advanced

Armali
  • 18,255
  • 14
  • 57
  • 171

1 Answers1

1

need some modifications here

extension TabBarCtr: SidebarViewDelegate {
    func sidebarDidSelectRow(row: Row) {
        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)
        }
        switch row {
        case .Name:
            print(" Name ")
        case .test:
            let test = TestLauncher()
            /// Comment below line
            test.show()

            /// Simply  Present it
            self.present(test, animated: true, completion: nil)
        case .test1:
            print("test1")
        case .none:
            break
        }
    }
}

Now in your TestLauncher

change

class TestLauncher: NSObject

to

class TestLauncher: UIViewController

Add View Life Cycles and to retain your animation

override func viewDidAppear(_ animated: Bool) {
    self.view.backgroundColor = .white
    self.testContainer.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
    self.view.addSubview(self.testContainer)
    self.testContainer.addSubview(self.btnClose)
    UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping: 1, initialSpringVelocity:1, options: .curveEaseOut, animations: {
        self.testContainer.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

    }, completion: nil)
}

Button Action

@objc func handleDismiss1(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}
iOS Geek
  • 4,825
  • 1
  • 9
  • 30