UIView
MenuBar
というカスタムがあります。このメニューバーは、カスタムの「タブバー」で、内部にcollectionView
があり、5つのセルがあります。セルをタップすると、私はViewController
を提示したいが、私は現在の機能にアクセスすることはできませんので、私は私のdidSelect
方法で以下のコードのようなものを呼び出すことはできません。UITabBarControllerのカスタムコレクションビューの使用
if indexPath.item == 1 {
let dummyController = DummyController()
present(dummyController, animated: true, completion: nil)
}
誰もが解決策を持っていますか?どんな助けも高く評価されます。君たちありがとう。私のコードは以下の通りです:
class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
cv.delegate = self
cv.dataSource = self
return cv
}()
let seperatorView: UIView = {
let view = UIView()
view.backgroundColor = lightGray
return view
}()
let imageNames = ["home_selected", "glimpse_selected", "camera_selected", "activity_selected", "profile_selected"]
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(collectionView)
addSubview(seperatorView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "cellId")
let selectedIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)
_ = collectionView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
_ = seperatorView.anchor(collectionView.topAnchor, left: collectionView.leftAnchor, bottom: nil, right: collectionView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 1)
setupHorizontalBar()
}
var horizontalBarLeftAnchorConstraint: NSLayoutConstraint?
func setupHorizontalBar() {
let horizontalBarView = UIView()
horizontalBarView.translatesAutoresizingMaskIntoConstraints = false
horizontalBarView.backgroundColor = .darkGray
addSubview(horizontalBarView)
horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor)
horizontalBarLeftAnchorConstraint?.isActive = true
horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/5).isActive = true
horizontalBarView.heightAnchor.constraint(equalToConstant: 4).isActive = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let x = CGFloat(indexPath.item) * frame.width/5
horizontalBarLeftAnchorConstraint?.constant = x
UIView.animate(withDuration: 0.45, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! MenuCell
cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width/5, height: self.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
上記のコードで例を挙げてください。私は少し初心者です。申し訳ありませんが痛みです。あなたの答えをありがとう!心から感謝する。トップマン – Jimmy
MenuBarファイルの最上部にプロトコルを追加し、そのプロパティをmenuBarに追加するだけです。次に、MenuBar自体を作成してビュー階層に追加する場合は、そのビューコントローラにデリゲートを設定し、プロトコル定義に必要な機能を実装します。 https://developer.apple.com/library/prerelease/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegationhtml https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html Objective-C but relevant – Tim