2016-12-04 8 views
1

iMessageグループチャットで見られるように、ナビゲーションバー内にコレクションビューを配置しようとしています。 。iMessageグループチャットのようにナビゲーションバーにコレクションビューを配置する方法

私は答えがhereであり、iOS10/Swift 3に変換するのに最善を尽くしましたが、コレクションビューのセルが正しくナビゲーションバーに表示されません。

import UIKit 


weak var collectionView: UICollectionView? 

class ChatController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let layout = UICollectionViewFlowLayout() 
    let collectionView = UICollectionView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(300), height: CGFloat(80)), collectionViewLayout: layout) 
    collectionView.backgroundColor = UIColor.clear 
    navigationItem.titleView? = collectionView 
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") 
    collectionView.delegate? = self 
    collectionView.dataSource? = self 
    view.addSubview(collectionView) 
    collectionView.reloadData() 
} 


func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 5 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)) 
    cell.backgroundColor = UIColor.orange 
    cell.layer.cornerRadius = 24 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: CGFloat(50), height: CGFloat(50)) 
} 

} 

とナビゲーションバークラス:ここに私のコードです

import Foundation 
import UIKit 



class NavigationBar: UINavigationBar { 

var chatController: ChatController? 

override func sizeThatFits(_ size: CGSize) -> CGSize { 
    return CGSize(width: CGFloat(self.superview!.bounds.size.width), height: CGFloat(80)) 
} 

func setFrame(frame: CGRect) { 
    var f = frame 
    f.size.height = 80 
    super.frame = f 
} 


} 

それは、ビューの細胞が現れているのコレクションのように見えますが、彼らは代わりに内部のナビゲーションバーの下にしている(下記参照画像)。ナビゲーションバーを非表示にすると、セルはビューの一番上に表示されますが、ナビゲーションバー内に表示されます。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 

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

    return true 
} 

私はこのプログラムで(何のストーリーボードを)やってないよ:私はここで初心者のミスを作ってるんだ場合

enter image description here

はここで、必ずアプリデリゲートの私のUIのコードではありません。誰かが私が間違っているのを見ることができますか?ここで

+0

'viewDidLoad'の最後の呼び出しとして' view.addSubview(collectionView)collectionView.reloadData() 'を呼び出します。ナビゲーションバーを追加するコードを投稿します。 – shallowThought

+0

ありがとう、私はサブビューを追加するのを忘れました..残念ながらそれはそれを修正しません!まだプレーンビューのコントローラ/ナビゲーションバーです。 – KingTim

+0

ChatControllerの追加方法を表示します。 – shallowThought

答えて

0

は、私は、これは動作するはずと考えているドキュメントからApple

If you want to display custom views in the navigation bar, you must wrap those views inside a UIBarButtonItem object before adding them to the navigation item.

For information about how navigation items work together with a navigation controller, custom view controllers, and the navigation bar to display their content, see View Controller Programming Guide for iOS .

weak var collectionView: UICollectionView? 
override func viewDidLoad() { 
    super.viewDidLoad() 
    let navigationitem = UINavigationItem(title: "") //creates a new item with no title 
    navigationitem.titleView = collectionView //your collectionview here to display as a view instead of the title that is usually there 
    self.navigationController?.navigationBar.items = [navigationitem] //adds the navigation item to the navigationbar 
} 

から、私の提案された解決策

です。コレクションビューのセルは、ナビゲーションバーに収まるくらい小さなものにしてください。私はそれが動作しない場合、多分私は明日何かを動作させることができます知ってみましょう。

+0

それは本当に興味深い、私はあなたがバーのボタンの項目でビューをラップする必要があることを認識していない、私はいくつかのより多くの読書を行うだろう...しかし私は私のOPで参照している例(obj- c)それをしないでうまく働いた。あなたのコードを試してみましたが、残念ながらクラッシュしました。エラーが発生しました:***キャッチされない例外 'NSInternalInconsistencyException'のためアプリを終了します。理由: 'setItemsを呼び出せません:アニメーション:コントローラによって管理されるUINavigationBarで直接発生します。 – KingTim

+0

Googleのエラーとすぐに解決策を見つけることができます。 –

+0

あなたはそれがあなたのために働いた結果を共有できますか?私はエラーを見つけて、結果の最初のページで解決策を試しましたが、問題は依然として続きます。 – KingTim

関連する問題