gpt4 book ai didi

swift 3 : Creating a custom TabBar with UICollectionView that segues to different controllers

转载 作者:行者123 更新时间:2023-11-30 12:40:17 25 4
gpt4 key购买 nike

我在一个有 5 个单元格的 UIView 类中创建了一个 collectionView。它看起来就像一个 TabBar,但通过 collectionView 来实现,我将能够根据我的喜好自定义它,对吧?我在我的 appDelegate 中设置了这个 menuBar,因此它将位于每个 View 的顶部。我遇到的问题是,在我的 collectionView 内部,我无法在 UIView 内部调用它:

present(viewController), animated: true, completion: nil)

我会在 didSelectItemAt 方法中调用它,并检查 indexPath.item 是否 == 0,继续到第一个 Controller ,但我似乎无法找到如何执行此操作的解决方案...这是我的代码。任何帮助将不胜感激,当然,标记为答案。干杯。

应用程序委托(delegate):

    var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: DummyController())

let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()

window?.addSubview(menuBar)

_ = menuBar.anchor(nil, left: window?.leftAnchor, bottom: window?.bottomAnchor, right: window?.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 50)

return true
}

MenuBar类(自定义标签栏):

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
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) {

if indexPath.item == 1 {
// segue
}

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")
}
}

最佳答案

您不能从 UIView(或任何其他非 Controller 类)类中呈现 UIViewController。但您可以将其添加为 subview 。要呈现或推送 UIViewcontroller,您必须从 UIViewController 类型的类中执行此操作。

关于 swift 3 : Creating a custom TabBar with UICollectionView that segues to different controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com