ios - 无法在 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()
}
}
解决方案
我认为你需要设置
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()
}}
推荐阅读
- sql - 当我尝试在我的 aws rds 中创建新角色时,如何修复 postgres'>
- pygame - 有没有一种简单的方法可以让点击在pygame中大于一个像素?
- docker - laravel-with-docker-example 项目无法构建
- macos - Cronjob 不会在 Anaconda (Mac) 上执行
- python - 在 HPC 上运行 jupyter notebook
- java - 回收站视图不是第一次加载,但在我刷新页面时它会立即加载
- visual-studio-code - 如何在 VS Code 的集成终端中设置下拉视图?
- android - 安卓对讲;如何禁用宣布细胞位置和细胞计数?
- java - 在 s:select 下拉列表中使用动态值,基于 Struts 2 中区域设置的资源包
- rust - Staking-pool 核心合约 - ext_contract 宏和回调