我在其他collectionView
中建立了collectionViewCell
,但我做不到
self.navigationController?.pushViewController(UIViewController(), animated: true)
我该怎么办?
答案 0 :(得分:0)
因为您必须在推送之前实例化viewController。
首先,您必须确定自己是否有navigationController
,然后将viewController
设为虚假
查看示例
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainVC") as UIViewController
navigationController?.pushViewController(vc, animated: true)
答案 1 :(得分:0)
导入UIKit class ItemDetailsHeaderCell:UICollectionViewCell {
let upperTab: UIView = {
let v = UIView()
v.backgroundColor = .white
return v
}()
let profileImage : UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "user")
image.layer.cornerRadius = 20
image.layer.masksToBounds = true
return image
}()
let usernameLbl : UIButton = {
let lbl = UIButton()
lbl.setTitle("Ali Abdalla Mansour", for: [])
lbl.titleLabel?.font = UIFont.systemFont(ofSize: 15)
// lbl.textColor = main_color
lbl.setTitleColor(main_color, for: [])
lbl.titleLabel?.textAlignment = .left
return lbl
}()
let optionsMenu : UIImageView = {
let img = UIImageView()
img.image = #imageLiteral(resourceName: "comments")
return img
}()
let seprator : UIView = {
let lbl = UIView()
lbl.backgroundColor = .gray
return lbl
}()
let adImage : UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "keyboard")
image.contentMode = .scaleToFill
// image.layer.cornerRadius = 20 // image.layer.masksToBounds = true 返回图片 }()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
setupUpperTab()
addSubview(upperTab)
addSubview(adImage)
upperTab.anchor(topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 50)
adImage.anchor(upperTab.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 270)
}
func setupUpperTab() {
upperTab.addSubview(profileImage)
upperTab.addSubview(usernameLbl)
upperTab.addSubview(optionsMenu)
upperTab.addSubview(seprator)
profileImage.anchor(upperTab.topAnchor, left: upperTab.leftAnchor, bottom: upperTab.bottomAnchor, right:usernameLbl.leftAnchor, topConstant: 5 , leftConstant: 12, bottomConstant: 5, rightConstant: 8, widthConstant: 40, heightConstant: 40)
usernameLbl.anchor(upperTab.topAnchor, left: profileImage.rightAnchor , bottom: upperTab.bottomAnchor, right: optionsMenu.leftAnchor, topConstant: 15, leftConstant: 8, bottomConstant: 15, rightConstant: 8, widthConstant: 0, heightConstant: 0 )
optionsMenu.anchor(upperTab.topAnchor, left: usernameLbl.rightAnchor, bottom: upperTab.bottomAnchor, right: upperTab.rightAnchor, topConstant: 15, leftConstant: 8, bottomConstant: 15, rightConstant: 12, widthConstant: 20, heightConstant: 20)
seprator.anchor(nil, left: upperTab.leftAnchor, bottom: upperTab.bottomAnchor, right: upperTab.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 1)
usernameLbl.addTarget(self, action: #selector(toUserProfile), for: .touchDown)
}
@objc func toUserProfile() {
let controller = UserProfileViewController(collectionViewLayout: UICollectionViewFlowLayout())
// loadingProgress.show(view: self.view)
controller.user_id = "1"
UINavigationController().pushViewController(controller, animated: true)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}