Chandler Long
Chandler Long

Reputation: 77

Unable to add UILabel to UIView in Swift 4

I am creating a custom Hamburger Menu with slide in and out functionality. I have the UIView sliding in and out appropriately but I am unable to add labels to my menu.

In my Menu Launcher class, I animate the view in. This is where I establish my view and where I would like to add my UILabel. The blackView is just for dimming the background upon presentation of the menu.

Under the function setUpViews() is where I am trying to add my UILabel but upon running the simulator nothing us showing up.

Menu Launcher

class MenuLauncher: UIView {
let cellId = "cellId"
let blackView = UIView()

let menuView: UIView = {
    let view = UIView()
    view.backgroundColor = Color.defaultBackgrondColor
    return view
}()

let nameLabel: UILabel = {
    let text = UILabel()
    text.text = "Chandler Long"
    text.font =  UIFont(name: "Avenir Next", size: 24.0)
    text.textColor = Color.darkBlue

    return text
}()

@objc func showMenu() {

    //Cover the entire window with UIView
    if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {

        blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

        //Adding Gesture to View
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleDismiss(_:)))
        blackView.addGestureRecognizer(tap)
        blackView.isUserInteractionEnabled = true

        window.addSubview(blackView)
        window.addSubview(menuView)

        let width = window.frame.width - 100

        //Initial animation size
        menuView.frame = CGRect(x: 0, y: 0, width: window.frame.width, height: window.frame.height)

        blackView.frame = window.frame
        blackView.alpha = 0

        //Animate In
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.blackView.alpha = 1

            self.menuView.frame = CGRect(x: 0, y: 0, width: width, height: window.frame.height)
        }, completion: nil)
    }
}

@objc func handleDismiss(_ sender: UITapGestureRecognizer) {
    UIView.animate(withDuration: 0.5) {
        self.blackView.alpha = 0

        //Animate Menu Out
        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            self.menuView.frame = CGRect(x: 0, y: 0, width: 0, height: window.frame.height)
        }
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)

    setUpView()
    print("ARE YOU HERE")

}

func setUpView() {

    menuView.addSubview(nameLabel)

    nameLabel.centerXAnchor.constraint(equalTo: menuView.centerXAnchor).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: menuView.centerYAnchor).isActive = true

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

This is where I present my menu

HomeController

class HomeController: UIViewController {
let cellId = "cellId"
let menuLauncher = MenuLauncher()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(HomeView(frame: view.frame))

}

@objc func handleMenuTap() {
    menuLauncher.showMenu()
}

}

Upvotes: 0

Views: 1044

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

I think you need to set

self.nameLabel.translatesAutoresizingMaskIntoConstraints = false

also before you add the label , you should auto-layout menuView

//

this line

let menuLauncher = MenuLauncher()

doesn't give a frame to the launcherView in the VC's self.view , so give it a frame inside viewDidLoad and add it like this

class HomeController: UIViewController {
   let cellId = "cellId"
   var menuLauncher:MenuLauncher?

override func viewDidLoad() {
    super.viewDidLoad()

    menuLauncher.frame = CGRect.init(x:-view.frame.size.width , y:20 ,width:view.frame.size.width, height:self.view.frame.height-20)
    view.addSubview(menuLauncher)

}

@objc func handleMenuTap() {
    menuLauncher.showMenu()
}}

Upvotes: 1

Related Questions