iOS:如何定制JSQMessagesCollectionview?

iOS:如何定制JSQMessagesCollectionview?,ios,swift,messaging,jsqmessagesviewcontroller,Ios,Swift,Messaging,Jsqmessagesviewcontroller,我想定制jsqMessageCollectionView。我想在底部视图中添加一些填充,以便滚动视图从一些底部边距开始 我需要这个,因为我的观点是重叠的。以下是截图: 第二件事我想定制时间戳。我想添加一个类似Facebook的功能。以下是场景: 显示日期和时间。对于在一分钟内发送的邮件,请使用“Just” 现在“而不是实际的时间戳 对于在同一天发送的邮件,请使用今天、时间。例如: 今天下午4:47 对于昨天发送的消息,请使用昨天的时间。例如: 昨天早上5点01分 对于其他内容,请使用标准格式:

我想定制
jsqMessageCollectionView
。我想在底部视图中添加一些填充,以便滚动视图从一些底部边距开始

我需要这个,因为我的观点是重叠的。以下是截图:

第二件事我想定制时间戳。我想添加一个类似Facebook的功能。以下是场景:

  • 显示日期和时间。对于在一分钟内发送的邮件,请使用“Just” 现在“而不是实际的时间戳
  • 对于在同一天发送的邮件,请使用今天、时间。例如: 今天下午4:47
  • 对于昨天发送的消息,请使用昨天的时间。例如: 昨天早上5点01分
  • 对于其他内容,请使用标准格式:日期、时间。例如: 2017年4月12日下午4:01
  • 我想定制时间戳标签,并在其中添加“刚才”文本。可能吗

    我可以更改视图中的时间和地点吗?我想显示气泡下面的时间戳


    提前谢谢。如果可以,请提供帮助?

    要在气泡下方显示时间戳,可以使用
    cellBottomLabel
    Cell的标签 在
    cellForItemAt indexPath
    方法中

    cell1.cellBottomLabel.text=“刚才”

    您还需要提供该标签的高度,如下所示

    override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellBottomLabelAt indexPath: IndexPath!) -> CGFloat {
        return 20.0
    }
    
    此外,要自定义时间戳,您可以始终使用日期格式化程序扩展。就我而言,我使用了以下方法

    或者您可以使用其他日期格式,如此处所示

    最后,对于按钮,您可以在顶部栏中显示它们,如果您愿意的话


    希望它有助于在气泡下方显示时间戳,您可以使用
    cellBottomLabel
    Cell的标签 在
    cellForItemAt indexPath
    方法中

    cell1.cellBottomLabel.text=“刚才”

    您还需要提供该标签的高度,如下所示

    override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellBottomLabelAt indexPath: IndexPath!) -> CGFloat {
        return 20.0
    }
    
    此外,要自定义时间戳,您可以始终使用日期格式化程序扩展。就我而言,我使用了以下方法

    或者您可以使用其他日期格式,如此处所示

    最后,对于按钮,您可以在顶部栏中显示它们,如果您愿意的话


    希望对您有所帮助

    时间戳问题已在上述答案中解决。 我刚刚添加了以下代码:

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
            cell.textView.delegate = self
            let message = messages[indexPath.item]
    
           // let formatter = DateFormatter()
             // time zone if needed
    
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
        let now = formatter.string(from: message.date as Date)
    
        let messageUTCDate = formatter.date(from: now)
    
        cell.cellBottomLabel.text =  formatter.timeAgoSinceDate(messageUTCDate!)
        return cell
    }
    
    我也能解决上述问题,以下是解决方案:

    为了在集合视图中添加按钮,我使用了以下代码,它将被添加到ChatViewController.swift中:

     func addSocialPagesButtonsToView()
        {
            var scrollView : UIScrollView!
    
            let pageNames = NSArray(objects: "Open My Facebook Page","Open My TripAdvisor Page")
            scrollView = UIScrollView(frame:CGRect(x:0, y:10, width:Int(collectionView.frame.width+100), height:50))
            scrollView.sizeToFit()
          //  scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
            for i in 0..<2 {
    
               // let catogry = categoryList[i] as! CategoryModel
                defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 200
                scrollView.addSubview(self.createViewForSocialPagesButtons(name: pageNames[i] as! String, heightOfParent: Int(scrollView.frame.size.height), tag: i))
                scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)
    
            }
            defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 40
            scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)
            scrollView.showsHorizontalScrollIndicator = true
            scrollView.showsVerticalScrollIndicator = false
    
            socialbuttonsScrollView.addSubview(scrollView)
        }
        func createViewForSocialPagesButtons(name: String, heightOfParent: Int, tag: Int) -> UIButton {
    
            let button = UIButton(frame: CGRect(x:defaultXValueforView , y: 0, width: 220, height: 40))
            button.backgroundColor = UIColor.clear
            button.setTitle(name, for: .normal)
            button.tag = tag
            button.setTitleColor(UIColor.blue, for: .normal)
            button.layer.cornerRadius = 2;
            button.layer.borderWidth = 1;
            button.layer.borderColor = UIColor.blue.cgColor
            button.titleLabel?.textAlignment = NSTextAlignment.center
            let font = UIFont(name: "Rubik", size: 17)
            button.titleLabel?.font = font
            button.addTarget(self, action: #selector(openPageButtonTapped(_:)), for: .touchUpInside)
    
            defaultXValueforView = defaultXValueforView + 225;
            return button
        }
    
        func openPageButtonTapped(_ sender : UIButton){
            let buttonTapped = sender.tag
            if(Commons.connectedToNetwork())
            {
                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "webVC") as! WebPageViewController
                if(buttonTapped==0)
                {
                    nextViewController.pageUrlAdress = "https://www.facebook.com/TripAdvisor/"
                }
                else if(buttonTapped==1)
                {
                  nextViewController.pageUrlAdress = "https://www.tripadvisor.com.ph/Restaurant_Review-g298460-d10229090-Reviews-Vikings_SM_City_Cebu_Restaurant-Cebu_City_Cebu_Island_Visayas.html"
                }
    
                self.navigationController?.pushViewController(nextViewController, animated:true)
    
            }
            else
            {
                Commons.showAlert("Please check your connection", VC: self)
            }
        }
    
    只需添加常量值(您希望减少的面积)
    bottom+您的值
    。例如:
    bottom+60

    我希望它能帮助所有想这样做的游客。如果有任何人有问题,请让我知道


    谢谢。

    时间戳问题已在上述答案中解决。 我刚刚添加了以下代码:

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
            cell.textView.delegate = self
            let message = messages[indexPath.item]
    
           // let formatter = DateFormatter()
             // time zone if needed
    
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
        let now = formatter.string(from: message.date as Date)
    
        let messageUTCDate = formatter.date(from: now)
    
        cell.cellBottomLabel.text =  formatter.timeAgoSinceDate(messageUTCDate!)
        return cell
    }
    
    我也能解决上述问题,以下是解决方案:

    为了在集合视图中添加按钮,我使用了以下代码,它将被添加到ChatViewController.swift中:

     func addSocialPagesButtonsToView()
        {
            var scrollView : UIScrollView!
    
            let pageNames = NSArray(objects: "Open My Facebook Page","Open My TripAdvisor Page")
            scrollView = UIScrollView(frame:CGRect(x:0, y:10, width:Int(collectionView.frame.width+100), height:50))
            scrollView.sizeToFit()
          //  scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
            for i in 0..<2 {
    
               // let catogry = categoryList[i] as! CategoryModel
                defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 200
                scrollView.addSubview(self.createViewForSocialPagesButtons(name: pageNames[i] as! String, heightOfParent: Int(scrollView.frame.size.height), tag: i))
                scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)
    
            }
            defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 40
            scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)
            scrollView.showsHorizontalScrollIndicator = true
            scrollView.showsVerticalScrollIndicator = false
    
            socialbuttonsScrollView.addSubview(scrollView)
        }
        func createViewForSocialPagesButtons(name: String, heightOfParent: Int, tag: Int) -> UIButton {
    
            let button = UIButton(frame: CGRect(x:defaultXValueforView , y: 0, width: 220, height: 40))
            button.backgroundColor = UIColor.clear
            button.setTitle(name, for: .normal)
            button.tag = tag
            button.setTitleColor(UIColor.blue, for: .normal)
            button.layer.cornerRadius = 2;
            button.layer.borderWidth = 1;
            button.layer.borderColor = UIColor.blue.cgColor
            button.titleLabel?.textAlignment = NSTextAlignment.center
            let font = UIFont(name: "Rubik", size: 17)
            button.titleLabel?.font = font
            button.addTarget(self, action: #selector(openPageButtonTapped(_:)), for: .touchUpInside)
    
            defaultXValueforView = defaultXValueforView + 225;
            return button
        }
    
        func openPageButtonTapped(_ sender : UIButton){
            let buttonTapped = sender.tag
            if(Commons.connectedToNetwork())
            {
                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "webVC") as! WebPageViewController
                if(buttonTapped==0)
                {
                    nextViewController.pageUrlAdress = "https://www.facebook.com/TripAdvisor/"
                }
                else if(buttonTapped==1)
                {
                  nextViewController.pageUrlAdress = "https://www.tripadvisor.com.ph/Restaurant_Review-g298460-d10229090-Reviews-Vikings_SM_City_Cebu_Restaurant-Cebu_City_Cebu_Island_Visayas.html"
                }
    
                self.navigationController?.pushViewController(nextViewController, animated:true)
    
            }
            else
            {
                Commons.showAlert("Please check your connection", VC: self)
            }
        }
    
    只需添加常量值(您希望减少的面积)
    bottom+您的值
    。例如:
    bottom+60

    我希望它能帮助所有想这样做的游客。如果有任何人有问题,请让我知道


    谢谢。

    谢谢你的帮助,伙计。请再告诉我一件事,我们能从左到右给底部标签留边距吗@wasim malekyou需要根据您的要求修改JSQ库的.XIB文件。您可以在资源文件夹中找到我正在使用的pods中的所有xib。。我仍然可以修改xib文件吗?我仍然停留在第二部分(按钮与集合视图重叠),您有什么解决方案吗?您可以从那里删除按钮并将其添加到顶部的导航栏中,使用
    UIAlertController
    即可。我不知道是否可以移动集合视图,因为它嵌入在
    jsqMessageViewController
    中。感谢您的帮助。请再告诉我一件事,我们能从左到右给底部标签留边距吗@wasim malekyou需要根据您的要求修改JSQ库的.XIB文件。您可以在资源文件夹中找到我正在使用的pods中的所有xib。。我仍然可以修改xib文件吗?我仍然停留在第二部分(按钮与集合视图重叠),您有什么解决方案吗?您可以从那里删除按钮并将其添加到顶部的导航栏中,使用
    UIAlertController
    即可。我不知道是否可以移动集合视图,因为它嵌入在
    jsqMessageViewController
    中。