0

5個のセルを持つUIViewクラス内にcollectionViewを作成しました。それはTabBarのように見えますが、collectionViewを使用してそれを行うと、私はそれを自分の心のコンテンツにカスタマイズすることができますか?私はこのメニューバーを私のappDelegateの中に設定して、すべてのビューの上に表示します。私が持っている問題は、私のcollectionViewの内側に、私はUIViewの内部でこれを呼び出すことはできませんよということです。Swift 3:別のコントローラに接続するUICollectionViewを使用してカスタムTabBarを作成する

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

私はセグエ、didSelectItemAtメソッド内でこれを呼び出し、indexPath.itemが== 0であるかどうかを確認します最初のコントローラには、私はこれを行う方法の解決策を見つけることができないようだ...ここで私のコードです。どんな助けでも非常に高く評価され、もちろん答えとしてマークされます。乾杯。

アプリの委任:

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

私はときどきタブバーの置換としてカスタムビューを追加しましたが、通常はこれをやっていました:** 1 **。すべてのタブコントローラーの表示領域になるRootViewControllerを作成します。 ** 2 **。根底にあるメニュー項目、または必要に応じて** 3 **を使ってカスタムビューを設定します。タップイベントのカスタムビューとルートビューコントローラの間にデリゲートを設定します。 ** 4 **。タップが受信されると、新しいビューコントローラをルートビューコントローラのサブビューとして追加します。ここには、タブバーコントローラのプレースホルダがあります。アニメーションが必要な場合 – GoodSp33d

+0

@Jimmyこのコードを試してくださいUIApplication.shared.keyWindow?.rootViewController?.present(ViewController、animated:true、completion:nil) –

答えて

0

あなたはUIViewの(または任意の他の非コントローラクラス)クラスからのUIViewControllerを提示することはできません。しかし、それをサブビューとして追加することはできます。 UIViewControllerを表示またはプッシュするには、UIViewController型のクラスから行う必要があります。

0

あなたはいくつかのオプションを持っている、あなたは、あなたのdidSelectItemAt方法であなたが持っているだろう、あなたのAppDelegateにあなたがそれに

menuBar.cellWithIndexSelected = { someIndex in 
    window?.rootViewController = UINavigationController(rootViewController: SomeOtherViewController()) 
} 

をいくつかの機能を付けることができ、バック

var cellWithIndexSelected : ((IndexPath) -> Void)? 

を呼び出しを使用することができますこの

if indexPath.item == 1 { 
// segue 
    cellWithIndexSelected(indexPath)  
} 

あなたはNotificationCenterを使用することもできますし、プロトを使用することもできますcol代表パターン。

これらは動作しますが、一般的には、このタブバーを保持している親ビューコントローラがあり、これらのプレゼンテーションをアプリケーションデリゲートの外部で処理する必要があります。

関連する問題