Swift 我可以在侧集合视图单元格中按ViewController(UIViewController(),动画:true)
我在otherSwift 我可以在侧集合视图单元格中按ViewController(UIViewController(),动画:true),swift,uicollectionview,uicollectionviewcell,Swift,Uicollectionview,Uicollectionviewcell,我在othercollectionViewCell中构建了collectionView,但我无法生成 self.navigationController?.pushViewController(UIViewController(), animated: true) 我该怎么办?因为在推送之前必须实例化viewController 首先,您必须确保是否有navigationController,然后才能安装viewController 参见一个示例 let storyboard = UIStor
collectionViewCell
中构建了collectionView
,但我无法生成
self.navigationController?.pushViewController(UIViewController(), animated: true)
我该怎么办?因为在推送之前必须实例化viewController 首先,您必须确保是否有
navigationController
,然后才能安装viewController
参见一个示例
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainVC") as UIViewController
navigationController?.pushViewController(vc, animated: true)
导入UIKit
类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
返回图像
}()
}很抱歉,我无法从您想要推动VC的位置执行此操作。上下文不清楚,请详细说明您的问题并提供一些代码示例?好的,我在UICollectionViewCell类中创建按钮,我正在尝试为其添加目标按钮。addTarget(self,action:#selector(toUserProfile),例如:。触地)它内部不工作,此外,我没有使用故事板,我正在编写代码,我正在尝试将代码应用到企业档案功能中
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")
}