无法在Swift 4中将UILabel添加到UIView

时间:2018-05-14 00:15:17

标签: ios swift

我正在创建一个带有滑入和滑出功能的自定义汉堡菜单。我让UIView适当地滑入和滑出,但我无法在菜单中添加标签。

在我的Menu Launcher类中,我为视图设置了动画。这是我建立视图的位置以及我想添加UILabel的位置。 blackView仅用于在显示菜单时调暗背景。

在函数setUpViews()下我正在尝试添加我的UILabel但在运行模拟器时我们没有显示出来。

菜单启动器

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")
}

}

这是我提供菜单的地方

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

}

1 个答案:

答案 0 :(得分:1)

我认为你需要设置

self.nameLabel.translatesAutoresizingMaskIntoConstraints = false

在添加标签之前,您应该自动布局menuView

//

这一行

let menuLauncher = MenuLauncher()

没有在VC的自我视图中为launcherView提供一个框架,所以在viewDidLoad内面给它一个框架,然后像这样添加它

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