首页 > 解决方案 > 无法在 Swift 4 中将 UILabel 添加到 UIView

问题描述

我正在创建一个具有滑入和滑出功能的自定义汉堡菜单。我有 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")
}

}

这是我展示菜单的地方

家庭控制器

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

}

标签: iosswift

解决方案


我认为你需要设置

self.nameLabel.translatesAutoresizingMaskIntoConstraints = false

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

//

这条线

let menuLauncher = MenuLauncher()

没有在 VC 的 self.view 中给 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()
}}

推荐阅读