如何设置tabbar徽章?

时间:2015-09-03 18:45:51

标签: swift uitabbarcontroller

我只是想添加一个'!'如果有新项目,并在点击标签栏后将其删除。我无法将代码放在下面的地方。

基本上,当创建通知或新会话或更新现有会话时,我会喜欢!弹出tabbar徽章,一旦用户点击tabbar项目!走了。

func conversationViewController(viewController: ATLConversationViewController, didSendMessage message: LYRMessage) {
    println("Message sent!")

}




func conversationViewController(viewController: ATLConversationViewController, didFailSendingMessage message: LYRMessage, error: NSError?) {
    println("Message failed to sent with error: \(error)")
}

func conversationViewController(viewController: ATLConversationViewController, didSelectMessage message: LYRMessage) {
    println("Message selected")


}

// MARK - ATLConversationViewControllerDataSource methods

func conversationViewController(conversationViewController: ATLConversationViewController, participantForIdentifier participantIdentifier: String) -> ATLParticipant? {
    if (participantIdentifier == PFUser.currentUser()!.objectId!) {
        return PFUser.currentUser()!
    }
    let user: PFUser? = UserManager.sharedManager.cachedUserForUserID(participantIdentifier)
    if (user == nil) {
        UserManager.sharedManager.queryAndCacheUsersWithIDs([participantIdentifier]) { (participants: NSArray?, error: NSError?) -> Void in
            if (participants?.count > 0 && error == nil) {
                //self.addressBarController.reloadView()
                // TODO: Need a good way to refresh all the messages for the refreshed participants...

                self.reloadCellsForMessagesSentByParticipantWithIdentifier(participantIdentifier)

            } else {
                println("Error querying for users: \(error)")
            }
        }
    }
    return user
}

func conversationViewController(conversationViewController: ATLConversationViewController, attributedStringForDisplayOfDate date: NSDate) -> NSAttributedString? {
    let attributes: NSDictionary = [ NSFontAttributeName : UIFont.systemFontOfSize(14), NSForegroundColorAttributeName : UIColor.grayColor() ]
    return NSAttributedString(string: self.dateFormatter.stringFromDate(date), attributes: attributes as? [String : AnyObject])
}

func conversationViewController(conversationViewController: ATLConversationViewController, attributedStringForDisplayOfRecipientStatus recipientStatus: [NSObject:AnyObject]) -> NSAttributedString? {
    if (recipientStatus.count == 0) {
        return nil
    }
    let mergedStatuses: NSMutableAttributedString = NSMutableAttributedString()

    let recipientStatusDict = recipientStatus as NSDictionary
    let allKeys = recipientStatusDict.allKeys as NSArray
    allKeys.enumerateObjectsUsingBlock { participant, _, _ in
        let participantAsString = participant as! String
        if (participantAsString == self.layerClient.authenticatedUserID) {
            return
        }

        let checkmark: String = "✔︎"
        var textColor: UIColor = UIColor.lightGrayColor()
        let status: LYRRecipientStatus! = LYRRecipientStatus(rawValue: recipientStatusDict[participantAsString]!.unsignedIntegerValue)
        switch status! {
        case .Sent:
            textColor = UIColor.lightGrayColor()
        case .Delivered:
            textColor = UIColor.orangeColor()
        case .Read:
            textColor = UIColor.greenColor()
        default:
            textColor = UIColor.lightGrayColor()
        }
        let statusString: NSAttributedString = NSAttributedString(string: checkmark, attributes: [NSForegroundColorAttributeName: textColor])
        mergedStatuses.appendAttributedString(statusString)
    }
    return mergedStatuses;
}

// MARK - ATLAddressBarViewController Delegate methods methods


// MARK - ATLParticipantTableViewController Delegate Methods

func participantTableViewController(participantTableViewController: ATLParticipantTableViewController, didSelectParticipant participant: ATLParticipant) {
    println("participant: \(participant)")
    self.addressBarController.selectParticipant(participant)
    println("selectedParticipants: \(self.addressBarController.selectedParticipants)")
    self.navigationController!.dismissViewControllerAnimated(true, completion: nil)
}

func participantTableViewController(participantTableViewController: ATLParticipantTableViewController, didSearchWithString searchText: String, completion: ((Set<NSObject>!) -> Void)?) {
    UserManager.sharedManager.queryForUserWithName(searchText) { (participants, error) in
        if (error == nil) {
            if let callback = completion {
                callback(NSSet(array: participants as! [AnyObject]) as Set<NSObject>)
            }
        } else {
            println("Error search for participants: \(error)")
        }
    }
}

}

解决https://stackoverflow.com/a/29837976/2303865

1 个答案:

答案 0 :(得分:0)

假设您已将消息数量查询到名为计数的变量中,然后将此计数显示在第一个tabBarItem中。

var arrayOfTabBar = self.tabBarController?.tabBar.items as NSArray!
let tabItem = arrayOfTabBar.objectAtIndex(1) as! UITabBarItem
tabItem.badgeValue =  counts