我对快速发展相对较新,我面临着一个让我永远需要解决的问题,我需要一些帮助。我创建了一个侧面菜单,由于某种原因,无法填充默认菜单项。我正在使用UICollectionView来填充菜单项。我以编程方式创建了collectionview和item。以下是我的代码:
import UIKit
class MenuCell: UICollectionViewCell {
var nameLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
nameLabel = UILabel()
nameLabel.text = "Scan NFC"
contentView.addSubview(nameLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class MenuLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
let cellId = "cellId"
override init() {
super.init()
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
collectionView.dataSource = self
collectionView.delegate = self
print("inside init")
}
@objc func showMenu(){
//show menu items here
if let window = UIApplication.shared.keyWindow{
//add background color for when menu is shown
blackView.backgroundColor = UIColor(white: 0 , alpha: 0.5)
//set action handler for when user clicks anywher in the screen to dismiss the menu
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
//add collection view
window.addSubview(collectionView);
//determine position of menu
let height: CGFloat = 200
let y = window.frame.height - height
//in order to animate the menu you need to change the y value from y to window.frame.height
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: 200)
//add the subview to the whle window so that the background also covers the navigation bar
blackView.frame = window.frame
blackView.alpha = 0
//accelerated animation
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
//set default height and width of menu
self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}, completion: nil)
}
}
@objc func handleDismiss(){
UIView.animate(withDuration: 0.5, animations: {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow{
self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
})
}
//set default size of each element
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 50)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
return cell
}
}
的ViewController
override func viewDidLoad() {
super.viewDidLoad()
setupNavBarButtons()
//get_data_from_url("http://www.kaleidosblog.com/tutorial/tutorial.json")
// Do any additional setup after loading the view, typically from a nib.
}
func setupNavBarButtons(){
let barButtonItem = UIBarButtonItem(image: UIImage(named: "Plus-Icon-40"),
style: .plain,
target: self,
action: #selector(invokeMenu))
navigationItem.rightBarButtonItems = [barButtonItem]
}
let menuLauncher = MenuLauncher()
@objc func invokeMenu() {
menuLauncher.showMenu()
}