ios - 如何像 iMessage 群聊一样在导航栏中放置 Collection View

标签 ios swift xcode ios10

我试图在导航栏中放置一个 Collection View ,如 iMessage 群聊中所示,其中所有成员的姓名首字母都在导航栏中的圆圈中。

我找到了答案here就这样,我尽力将其转换为 iOS10/Swift 3,但 Collection View 单元格没有正确显示在导航栏中。这是我的代码:

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
}


}

看起来 Collection View 单元格已显示,但它们位于导航栏下方而不是导航栏内部(见下图)。如果我隐藏导航栏,单元格将转到 View 顶部,但我希望将它们放在导航栏中。

enter image description here

这是我在应用程序委托(delegate)中的 UI 代码,不确定我是否在这里犯了新手错误:

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
}

我正在以编程方式执行此操作(没有 Storyboard)。谁能看出我哪里出错了?

最佳答案

这是我提出的解决方案

来自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
}

从文档中我相信这应该可行。请记住让您的 Collection View 单元格足够小以适合导航栏。如果不起作用请告诉我,也许我明天可以解决一些问题。

关于ios - 如何像 iMessage 群聊一样在导航栏中放置 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40964118/

相关文章:

ios - 使用 nscoder 对自定义对象进行编码并保存到 nsuserdefaults 是否已加密?

swift - Swift 字符串中区分大小写的字符替换

ios - 在代码片段中包含重写

swift - SceneKit 相机没有移动到正确的位置

ios - UIPageViewController 不遵守 iOS 7 中的顶部布局指南

ios - 在 Swift 中将完成处理程序分配给闭包变量

ios - 缩放 UITextView 并保持文本清晰度

iphone - RSS 阅读器不在 Web View 中显示选定的单元格

ios - IOS 中的 Facebook 错误代码=7

ios - Swift - 使用闭包在 init 方法中设置属性