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

标签 ios swift

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

在我的 Menu Launcher 类中,我对 View 进行动画处理。这是我建立 View 的地方,也是我想添加 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")
}

这是我展示菜单的地方

家庭 Controller

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

最佳答案

我认为你需要设置

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

关于ios - 无法在 Swift 4 中将 UILabel 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321747/

相关文章:

iphone - 创建登录屏幕以链接到 iOS 应用程序中的表单

ios - 即使将样式更改为 UITableViewStyleGrouped 后,Tableview 标题也不可滚动

ios - 无法将类型 'UIColor' 的值分配给类型 'String?'

ios - CLLocation Manager startUpdatingLocation第二次调用不起作用

objective-c - iOS 后台应用程序间通信

ios - Swift:如何等待事件然后切换 View ?

json - 在此上下文中,“D”对于类型查找不明确

swift - 协议(protocol)不符合自身?

swift - 在 Swift 3 中访问代码错误

ios - 访问 AVAudioRecorder 的 meterEnabled 报错