Swift CVCalendar not updating monthLabel - ios

I recently started using CVCalendar to create a CalendarView with a Week presentation.
I created a UILabel called monthLabel which has an outlet to the view's controller
The ViewController for this particular view is this one:
import UIKit
import CVCalendar
class EventsViewController: UIViewController {
#IBOutlet weak var headerView: UIView!
#IBOutlet weak var menuView: CVCalendarMenuView!
#IBOutlet weak var calendarView: CVCalendarView!
#IBOutlet weak var eventsTable: UITableView!
#IBOutlet weak var monthLabel: UILabel!
private var currentCalendar: Calendar?
private var animationFinished: Bool = true
let mainAppViewHeader: MainAppViewHeader = UIView.fromNib()
override func viewDidLoad() {
super.viewDidLoad()
loadViewHeader()
setupTable()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
setupDatePicker()
}
override func awakeFromNib() {
let timeZoneBias = 480 // (UTC+08:00)
currentCalendar = Calendar(identifier: .gregorian)
currentCalendar?.locale = Locale(identifier: "fr_FR")
if let timeZone = TimeZone(secondsFromGMT: -timeZoneBias * 60) {
currentCalendar?.timeZone = timeZone
}
}
func setupDatePicker() {
menuView.delegate = self
calendarView.delegate = self
menuView.backgroundColor = ColorManager.Gray5
calendarView.backgroundColor = ColorManager.Gray5
calendarView.appearance.dayLabelPresentWeekdaySelectedBackgroundColor = ColorManager.PrimaryColor
calendarView.appearance.dayLabelWeekdaySelectedBackgroundColor = ColorManager.PrimaryColor
if let currentCalendar = currentCalendar {
monthLabel.text = CVDate(date: Date(), calendar: currentCalendar).globalDescription
}
menuView.commitMenuViewUpdate()
calendarView.commitCalendarViewUpdate()
}
func loadViewHeader() {
mainAppViewHeader.setupHeader()
headerView.addSubview(mainAppViewHeader)
}
func setupTable() {
let rowNib = UINib(nibName: "EventTableCell", bundle: nil)
eventsTable.dataSource = self
eventsTable.delegate = self
eventsTable.register(rowNib, forCellReuseIdentifier: "eventCell")
eventsTable.separatorStyle = .none
}
}
extension EventsViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
func presentationMode() -> CalendarMode {
return .weekView
}
func firstWeekday() -> Weekday {
return .sunday
}
func shouldAutoSelectDayOnWeekChange() -> Bool {
return true
}
func disableScrollingBeforeDate() -> Date {
let currentDate = Date()
return currentDate.addingTimeInterval(-8 * 24 * 60 * 60)
}
func disableScrollingBeyondDate() -> Date {
let currentDate = Date()
return currentDate.addingTimeInterval(180 * 24 * 60 * 60)
}
func maxSelectableRange() -> Int {
return 1
}
func shouldSelectRange() -> Bool {
false
}
func earliestSelectableDate() -> Date {
let currentDate = Date()
return currentDate.addingTimeInterval(-8 * 24 * 60 * 60)
}
func latestSelectableDate() -> Date {
let currentDate = Date()
return currentDate.addingTimeInterval(180 * 24 * 60 * 60)
}
func shouldShowWeekdaysOut() -> Bool {
return true
}
func didShowPreviousWeekView(from startDayView: DayView, to endDayView: DayView) {
print(startDayView, endDayView)
}
func presentedDateUpdated(_ date: CVDate) {
monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
print(date.globalDescription)
}
}
extension EventsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let eventCell = tableView.dequeueReusableCell(withIdentifier: "eventCell") as? EventTableCell else {
return UITableViewCell()
}
eventCell.setupCell()
eventCell.updateConstraintsIfNeeded()
return eventCell
}
}
extension EventsViewController: UITableViewDelegate {
}
And the outlet reference:
The issue here is that even tho I'm telling the UILabel to change its text whenever the presented date is updated:
func presentedDateUpdated(_ date: CVDate) {
monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
print(date.globalDescription)
}
It's not updating its value, yet I get the date.globalDescription value printed in the console.
Is there something I'm missing when trying to update the label?
I tried using the code in the official example:
func presentedDateUpdated(_ date: CVDate) {
if monthLabel.text != date.globalDescription && self.animationFinished {
let updatedMonthLabel = UILabel()
updatedMonthLabel.textColor = monthLabel.textColor
updatedMonthLabel.font = monthLabel.font
updatedMonthLabel.textAlignment = .center
updatedMonthLabel.text = date.globalDescription
updatedMonthLabel.sizeToFit()
updatedMonthLabel.alpha = 0
updatedMonthLabel.center = self.monthLabel.center
let offset = CGFloat(48)
updatedMonthLabel.transform = CGAffineTransform(translationX: 0, y: offset)
updatedMonthLabel.transform = CGAffineTransform(scaleX: 1, y: 0.1)
UIView.animate(withDuration: 0.35, delay: 0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.animationFinished = false
self.monthLabel.transform = CGAffineTransform(translationX: 0, y: -offset)
self.monthLabel.transform = CGAffineTransform(scaleX: 1, y: 0.1)
self.monthLabel.alpha = 0
updatedMonthLabel.alpha = 1
updatedMonthLabel.transform = CGAffineTransform.identity
}) { _ in
self.animationFinished = true
self.monthLabel.frame = updatedMonthLabel.frame
self.monthLabel.text = updatedMonthLabel.text
self.monthLabel.transform = CGAffineTransform.identity
self.monthLabel.alpha = 1
updatedMonthLabel.removeFromSuperview()
}
self.view.insertSubview(updatedMonthLabel, aboveSubview: self.monthLabel)
}
}
But this approach is only pushing a new view with the updated month and then dismissing the view, and while it contains basically the same code that I was using:
self.monthLabel.text = updatedMonthLabel.text
The monthLabel is not being updated, this is what happens when using the above code, it reverts back to original text.
If I remove this line:
updatedMonthLabel.removeFromSuperview()
Both texts get overlapped, so it's ignoring this line:
self.monthLabel.text = updatedMonthLabel.text
I've done some extra testing, and checked that the label is correctly linked with an outlet to my view, I changed the text color:
func presentedDateUpdated(_ date: CVDate) {
print(Thread.current)
print("Wololo")
monthLabel.textColor = UIColor.green
monthLabel.text = "asdf"
// calendarView.contentController.refreshPresentedMonth()
// monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
// print(date.globalDescription)
}
It prints:
<NSThread: 0x600002b6c100>{number = 1, name = main}
Wololo
N number of times I change the day by clicking on the calendar, however the text remains unchanged:

For some reason the code inside:
override func awakeFromNib() {
let timeZoneBias = 480 // (UTC+08:00)
currentCalendar = Calendar(identifier: .gregorian)
currentCalendar?.locale = Locale(identifier: "fr_FR")
if let timeZone = TimeZone(secondsFromGMT: -timeZoneBias * 60) {
currentCalendar?.timeZone = timeZone
}
}
was in conflict with not updating the label, if we remove the above code and instead do this:
func setupDatePicker() {
menuView.delegate = self
calendarView.delegate = self
menuView.commitMenuViewUpdate()
calendarView.commitCalendarViewUpdate()
monthLabel.text = calendarView.presentedDate.globalDescription //This line at the bottom of this method
}
Everything works once again.
If someone knows the reason behind it, feel free to make a comment and I'll add it to the answer.

presentedDateUpdated is likely being called on a background thread making it so your UI changes aren't being displayed. you can confirm this by printing Thread.current inside presentedDateUpdated.
a simple solution would be this:
func presentedDateUpdated(_ date: CVDate) {
DispatchQueue.main.async { [weak self] in
self?.monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
print(date.globalDescription)
}
}

Related

Autoadapt height of View

I m building a chat View in xCode with swift language.
I m create two custom cell one to sender message and another one to received message. The problem is when the text of message is greater than the width of the screen of iPhone. Then I need to configure the two views to increase the height if the text is too big.
So this is my view:
I configured the height of View Lbl Message >= 30 but if you see xCode set to RED that constraint and for me the error is in that part.
Anyway this is the result of my iPhone when application stil running:
If you check with more attention after the text "mio" in the blue cell you can see that there are other text but it is not displayed.
EDIT
I added ChatVC controller where is the UITableView:
//
// ChatVC.swift
// appUser
//
// Created by mac on 11/10/21.
// Copyright © 2021 Michele Castriotta. All rights reserved.
//
import UIKit
import SDWebImage
import SwiftyJSON
import AVFoundation
class ChatVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var aggiorna:Bool = false
var arrMsgs:[ChatModel] = []
var receiverId = ""
var storeName = ""
var userName = ""
var userId = ""
var strReason = ""
var strReasonID = ""
var strType = "User"
var strRighTitle = ""
var strPrname = ""
var daNotifica:Bool = false
#IBOutlet weak var viewWriteMessage: UIView!
#IBOutlet weak var lblTitleChat: UILabel!
#IBOutlet weak var txtMessage: UITextField!
#IBOutlet weak var tableViewChat: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.showProgressBar()
self.tableViewChat.separatorColor = UIColor.clear
self.viewWriteMessage.setCornerRadius(cornerRadius: 22, borderColor: nil, borderWidth: nil)
userId = kUserDefault.value(forKey: USERID) as! String
self.lblTitleChat.text = self.storeName
wsGetChatAgain()
}
func wsGetChatAgain() {
// showProgressBar()
var paramDict : [String:AnyObject] = [:]
paramDict["receiver_id"] = receiverId as AnyObject
paramDict["sender_id"] = userId as AnyObject
CommunicationManeger.callPostServiceReturnJson(apiUrl: RouterProd.get_chat.url(), parameters: paramDict, parentViewController: self, successBlock: { (responseData, message) in
DispatchQueue.main.async {
do {
let chats = try JSONDecoder().decode(ResponseChatModel.self, from: responseData as! Data)
if(chats.status == "1") {
self.arrMsgs = chats.result
DispatchQueue.main.async {
// Main thread, called after the previous code:
// hide your progress bar here
self.tableViewChat.reloadData()
self.hideProgressBar()
}
//self.scrollToBottom()
//self.lbl_ChatReason.text = self.strReason
}
}catch{
print("errore durante la decodifica dei dati: \(error)")
self.hideProgressBar()
//Utility.noDataFound("Errore", tableViewOt: self.tableViewChat, parentViewController: self)
}
}
}, failureBlock: { (error : Error) in
Utility.showAlertMessage(withTitle: EMPTY_STRING, message: (error.localizedDescription), delegate: nil,parentViewController: self)
self.hideProgressBar()
})
self.aggiorna = true
self.aggiornaChat()
}
func aggiornaChat(){
if(aggiorna){
DispatchQueue.main.asyncAfter(deadline: .now() + 15.0, execute: wsGetChatAgain)
}
}
#IBAction func btnBack(_ sender: Any) {
self.aggiorna = false
if(self.daNotifica == false){
self.navigationController?.popViewController(animated: true)
}else{
Switcher.updateRootVC()
}
}
//MARK: - Table View Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrMsgs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let message = self.arrMsgs[indexPath.row]
var messaggio = message.chatMessage
var orario = Utility.getDateStringFromString(dateString: message.date, inputFormate: "yyyy-MM-dd HH:mm:ss", outputFormate: "HH:mm")
if(message.senderID == userId){
//messaggio inviato dal utente
let cell = tableView.dequeueReusableCell(withIdentifier: "ChatCell", for: indexPath) as! ChatCell
cell.lblMessage.text = messaggio
cell.lblTime.text = orario
var maximumLabelSize: CGSize = CGSize(width: 280, height: 9999)
var expectedLabelSize: CGSize = cell.lblMessage.sizeThatFits(maximumLabelSize)
// create a frame that is filled with the UILabel frame data
var newFrame: CGRect = cell.lblMessage.frame
// resizing the frame to calculated size
newFrame.size.height = expectedLabelSize.height
// put calculated frame into UILabel frame
cell.lblMessage.frame = newFrame
return cell
}else{
//messaggio inviato dallo o runner
let cell = tableView.dequeueReusableCell(withIdentifier: "ChatCell2", for: indexPath) as! ChatCell2
cell.lblMessage.text = messaggio
var maximumLabelSize: CGSize = CGSize(width: 280, height: 9999)
var expectedLabelSize: CGSize = cell.lblMessage.sizeThatFits(maximumLabelSize)
// create a frame that is filled with the UILabel frame data
var newFrame: CGRect = cell.lblMessage.frame
// resizing the frame to calculated size
newFrame.size.height = expectedLabelSize.height
newFrame.size.width = expectedLabelSize.width
// put calculated frame into UILabel frame
cell.lblMessage.frame = newFrame
cell.lblTime.text = orario
return cell
}
}
#IBAction func sendMessage(_ sender: Any) {
if txtMessage.text == "Scrivi qui..." || txtMessage.text!.count == 0 {
GlobalConstant.showAlertMessage(withOkButtonAndTitle: APPNAME, andMessage: "Per favore scrivi del testo...", on: self)
} else {
wsSendMessage()
}
}
func wsSendMessage() {
self.showProgressBar()
var localTimeZoneIdentifier: String { return TimeZone.current.identifier }
var paramDict : [String:AnyObject] = [:]
paramDict["receiver_id"] = receiverId as AnyObject
paramDict["sender_id"] = userId as AnyObject
paramDict["chat_message"] = self.txtMessage.text! as AnyObject
paramDict["timezone"] = localTimeZoneIdentifier as AnyObject
paramDict["request_id"] = strReasonID as AnyObject
paramDict["type"] = strType as AnyObject
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
var dataOra = formatter.string(from:Date())
paramDict["date_time"] = "\(dataOra)" as AnyObject
CommunicationManeger.callPostService(apiUrl: RouterProd.insert_chat.url(), parameters: paramDict, parentViewController: self, successBlock: { (responseData, message) in
DispatchQueue.main.async {
let swiftyJsonVar = JSON(responseData)
print(swiftyJsonVar)
if(swiftyJsonVar["result"].stringValue == "successful") {
self.txtMessage.text = ""
self.view.endEditing(true)
self.wsGetChatAgain()
}
self.hideProgressBar()
}
}, failureBlock: { (error : Error) in
Utility.showAlertMessage(withTitle: EMPTY_STRING, message: (error.localizedDescription), delegate: nil,parentViewController: self)
self.hideProgressBar()
})
}
}
This is chatCell class
import UIKit
class ChatCell: UITableViewCell {
#IBOutlet weak var lblMessage: UILabel!
#IBOutlet weak var viewLblMessage: UIView!
#IBOutlet weak var lblTime: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.viewLblMessage.setCornerRadius(cornerRadius: 10, borderColor: nil, borderWidth: nil)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
If you don't know the size of the text, don't add height to the view. You should add your constraints without adding height

How to implement the ListCollectionViewLayout while using Texture?

I have been having trouble getting the ListCollectionViewLayout, the stock layout provided by IGListKit, to work together with Texture (formerly AsyncDisplayKit).
This is my Collection View Controller:
let collectionNode: ASCollectionNode!
var refreshControl : UIRefreshControl?
var layout: ListCollectionViewLayout
var pageTitle: String?
var feedItems: [FeedItem] = [FeedItem]()
lazy var adapter: ListAdapter = {
return ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 1)
}()
init() {
layout = ListCollectionViewLayout.init(stickyHeaders: false, scrollDirection: .vertical, topContentInset: 0, stretchToEdge: false)
self.collectionNode = ASCollectionNode(collectionViewLayout: layout)
super.init(node: self.collectionNode)
self.adapter.setASDKCollectionNode(self.collectionNode)
self.adapter.dataSource = self
self.collectionNode.alwaysBounceVertical = true
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: #selector(refreshContent), for: .valueChanged)
self.collectionNode.view.addSubview(refreshControl!)
}
This is the Section Controller:
class HashtagSectionController: ListSectionController, ASSectionController {
weak var delegate: HashtagDataDelegate?
var pushViewDelegate: PushViewControllerDelegate?
var pushUserDelegate: PushUsernameDelegate?
var isLoading: Bool
func nodeForItem(at index: Int) -> ASCellNode {
guard let feedItem = object else { return ASCellNode() }
let node = DiscoverCellNode(post: feedItem.post, user: feedItem.user)
DispatchQueue.main.async {
node.contentNode.delegate = self
}
return node
}
override init() {
self.isLoading = false
super.init()
self.inset = UIEdgeInsets(top: 10, left: 0, bottom: 20, right: 0)
}
var object: FeedItem?
func nodeBlockForItem(at index: Int) -> ASCellNodeBlock {
guard let feedItem = object else { return {
return ASCellNode()
}
}
return {
let node = DiscoverCellNode(post: feedItem.post, user: feedItem.user)
DispatchQueue.main.async {
node.contentNode.delegate = self
}
return node
}
}
override func numberOfItems() -> Int {
return 1
}
override func didUpdate(to object: Any) {
self.object = object as? FeedItem
}
override func didSelectItem(at index: Int) {
guard let feedItem = object else { return }
pushViewDelegate?.pushViewController(post: feedItem.post, user: feedItem.user)
}
override func sizeForItem(at index: Int) -> CGSize {
return ASIGListSectionControllerMethods.sizeForItem(at: index)
//return CGSize(width: 120, height: 120)
}
override func cellForItem(at index: Int) -> UICollectionViewCell {
return ASIGListSectionControllerMethods.cellForItem(at: index, sectionController: self)
}
}
So I am unsure as to why this isn't working. Am I missing a Delegate the ListCollectionLayout needs in order to work. I get an error stating "layoutSize is invalid and unsafe to provide to CoreAnimation".
I ended up not using the listkit and decided to use just the diffing algorithm with the Texture collection view.

Issue with Activity Indicator While loading data in UITableView

I'm getting record from server using php
As i'm getting 11 record currently so i want in starting i will show just 6 records and remaining next 5 record will show when user reached at last cell while scrolling. So this process is in working form, but the problem is while running, it working so fast that before reaching last row all records are already showing while scrolling and the activity indicator is just animating at the bottom of tableView.
I don't know what is the problem.
Also i want when user reached at last cell, activity indicator start animating while loading data .
Here is my code
import UIKit
import AlamofireImage
struct property{
let property_Id : String
let propertyTitle : String
let imageURL : String
let user_Id : String
let area : String
let bed : String
let unit : String
let bath : String
let price : String
}
class searchRecordsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,favouriteButtonTableViewCellDelegate {
var y = 6
var m = 12
#IBOutlet weak var tableView : UITableView!
var RESULT : [NSDictionary] = []
var myProperty = [property]()
var myPropertyCopy = [property]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: Getting dictionary data from previous controller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "Results"
self.navigationController?.navigationBar.tintColor = UIColor.black
//MARK: Getting dictionary data from previous controller
for item in RESULT {
let propertyInfo = property(property_Id: String(item["propertyId"]! as! Int), propertyTitle: item["propertyTitle"]! as! String, imageURL: item["imagePath"]! as! String, user_Id: String(item["userId"]! as! Int), area: item["area"]! as! String, bed: item["bed"]! as! String, unit: item["unit"]! as! String, bath: item["bath"]! as! String, price: item["price"]! as! String )
myProperty.append(propertyInfo)
}
//MARK: Inserting first 6 records in Array
for i in 0 ..< 6 {
if !(myProperty.indices.contains(i)) {
break
}
myPropertyCopy.append(myProperty[i])
}
}
func downloadImage(imagePath : String, theIMAGEVIEW : UIImageView) {
let myUrl = URL(string: URL_IP+imagePath);
//MARK: AlamofireImage to download the image
theIMAGEVIEW.af_setImage(withURL: myUrl!, placeholderImage: #imageLiteral(resourceName: "addProperty"), filter: nil, progress: nil, runImageTransitionIfCached: true, completion: nil)
}
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return myPropertyCopy.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "RecordCell") as! searchrecordsTableViewCell
let property = myPropertyCopy[indexPath.row]
cell.area.text = property.area+" "+property.unit
if property.bath == ""{
cell.bath.text = property.bath
}
else{
cell.bath.text = property.bath+" Baths"
}
if property.bed == ""{
cell.bed.text = property.bed
}
else{
cell.bed.text = property.bed+" Baths"
}
cell.propertyTitle.text = property.propertyTitle
cell.price.text = convertAMOUNT(price : property.price)
downloadImage(imagePath: property.imageURL, theIMAGEVIEW: cell.myImageView)
//----
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if myPropertyCopy.count != myProperty.count{
let lastRow = myPropertyCopy.count - 1
if indexPath.row == lastRow {
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableView.tableFooterView = spinner
self.tableView.tableFooterView?.isHidden = false
moreData()
}
}
}
func moreData(){
for i in y ..< m {
if !(myProperty.indices.contains(i)) {
break
}
myPropertyCopy.append(myProperty[i])
}
y = y + 10
m = m + 10
self.tableView.reloadData()
}
}
currently my TableView looks like
See output here
Thanks in Advance.
After a long practice i have done my problem, i have just added UIScrollView delegate function for checking if the user reached at last cell then start activityIndicator for 3 seconds and then load data and that's it.
As i have just very small amount of data that's why i'm start ing UIactivityIndicator for 3 seconds before getting data.
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// UITableView only moves in one direction, y axis
let currentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
if maximumOffset - currentOffset <= 10.0 {
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
if myPropertyCopy.count != myProperty.count {
//print("this is the last cell")
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
spinner.hidesWhenStopped = true
self.tableView.tableFooterView = spinner
self.tableView.tableFooterView?.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
//MARK: Loading more data
self.moreData()
}
}
else{
self.tableView.tableFooterView?.isHidden = true
spinner.stopAnimating()
}
}
}

How do I update text input position of the y depending on keyboard height in swift

I'm trying to implement the comment view controller.
I want using the auto layout via the storyboard.
My question is...
When I tap the input text..then keyboard will move up...but input text is not move up..
Keyboard is overlapping the text input..
Here is TableViewController.swift
import UIKit
import Parse
var commentUUID = [String]()
var commentOwner = [String]()
class CommentViewController: UIViewController, UITextViewDelegate, UITableViewDelegate, UITableViewDataSource{
//UI Objects
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var commentTextView: UITextView!
#IBOutlet weak var sendButton: UIButton!
var refresher = UIRefreshControl()
//values for reseting UI to default
var tableViewHeight : CGFloat = 0
var commentY : CGFloat = 0
var commentHeight : CGFloat = 0
//arryas to hold server data
var usernameArray = [String]()
var profileArray = [PFFile]()
var commentArray = [String]()
var dateArray = [NSDate?]()
//variable to hold keyboard frame
var keyboard = CGRect()
//page size
var page : Int32 = 15
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = .redColor()
//title at the top
self.navigationItem.title = "COMMENTS"
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(title: "back", style: .Plain, target: self, action: #selector(CommentViewController.back(_:)))
self.navigationItem.leftBarButtonItem=backButton
//swipe to go back
let backSwipe = UISwipeGestureRecognizer(target: self, action: #selector(CommentViewController.back(_:)))
backSwipe.direction=UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(backSwipe)
// catch notification if the keyboard is shown or hidden
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
//disable button from the beginning
sendButton.enabled = false
//call function
alignment()
loadComments()
}
//I think it is not affect on the layout...
func configureTableView() {
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0
}
// preload func
override func viewWillAppear(animated: Bool) {
//hide bottom bar
self.tabBarController?.tabBar.hidden = true
}
// postload func
override func viewWillDisappear(animated: Bool) {
self.tabBarController?.tabBar.hidden = false
}
//func loading when keyboard is shown
func keyboardWillShow(notification : NSNotification){
//define keyboard frame size
keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]!.CGRectValue)!
//move UI up
UIView.animateWithDuration(0.4){ () -> Void in
self.tableView.frame.size.height = self.tableViewHeight - self.keyboard.height - self.commentTextView.frame.size.height + self.commentHeight
print("keyboard show")
self.commentTextView.frame.origin.y = self.commentY - self.keyboard.height - self.commentTextView.frame.size.height + self.commentHeight
self.sendButton.frame.origin.y = self.commentTextView.frame.origin.y
self.commentTextView.frame.origin.y = 250
}
}
//func loading when keyboard is hidden
func keyboardWillHide(notification : NSNotification){
//move UI down
UIView.animateWithDuration(0.4){() -> Void in
self.tableView.frame.size.height = self.tableViewHeight
self.commentTextView.frame.origin.y = self.commentY
self.sendButton.frame.origin.y = self.commentY
}
}
//alignment function
func alignment(){
let width = self.view.frame.size.width
let height = self.view.frame.size.height
tableView.frame = CGRectMake(0, 0, width, height - self.navigationController!.navigationBar.frame.size.height)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0
commentTextView.layer.cornerRadius = commentTextView.frame.size.width / 50
//delegates
commentTextView.delegate = self
tableView.delegate = self
tableView.dataSource = self
//assign reseting values
tableViewHeight = tableView.frame.size.height
commentHeight = commentTextView.frame.size.height
commentY = commentTextView.frame.origin.y
}
//while writing something
func textViewDidChange(textView: UITextView) {
//disable button if entered no text
let spacing = NSCharacterSet.whitespaceAndNewlineCharacterSet()
//It shown when user entered type
if !commentTextView.text.stringByTrimmingCharactersInSet(spacing).isEmpty{
sendButton.enabled = true
}else {
sendButton.enabled = false
}
// + paragraph
if textView.contentSize.height > textView.frame.size.height && textView.frame.height < 130{
//find difference to add
let difference = textView.contentSize.height - textView.frame.size.height
//redefine frame of commentText
textView.frame.origin.y = textView.frame.origin.y - difference
textView.frame.size.height = textView.contentSize.height
//move up tableView
if textView.contentSize.height + keyboard.height + commentY >= tableView.frame.size.height {
tableView.frame.size.height = tableView.frame.size.height - difference
}
}
// - parapraph
else if textView.contentSize.height < textView.frame.size.height {
//find difference to deduct
let difference = textView.frame.size.height - textView.contentSize.height
//redefine frame of commentText
textView.frame.origin.y = textView.frame.origin.y + difference
textView.frame.size.height = textView.contentSize.height
//move down tableview
if textView.contentSize.height + keyboard.height + commentY > tableView.frame.size.height {
tableView.frame.size.height = tableView.frame.size.height + difference
}
}
}
//load comments function
func loadComments(){
//STEP 1. Count total comments in order to skip all except page size
let countQuery = PFQuery(className: "comments")
countQuery.whereKey("to", equalTo: commentUUID.last!)
countQuery.countObjectsInBackgroundWithBlock({(count:Int32, error:NSError?) -> Void in
//if comments on the server for current post are more than (page size 15) implement pull to refresh func
if self.page < count {
self.refresher.addTarget(self, action: #selector(CommentViewController.loadMore), forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(self.refresher)
}
//STEP 2. Request last (page size 15) comments
let query = PFQuery(className: "comments")
query.whereKey("to", equalTo: commentUUID.last!)
query.skip = count - self.page
query.addAscendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock({(objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
//clean up
self.usernameArray.removeAll(keepCapacity: false)
self.profileArray.removeAll(keepCapacity: false)
self.commentArray.removeAll(keepCapacity: false)
self.dateArray.removeAll(keepCapacity: false)
//find related object
for object in objects!{
self.usernameArray.append(object.objectForKey("username") as! String)
self.profileArray.append(object.objectForKey("profileImg") as! PFFile)
self.commentArray.append(object.objectForKey("comment") as! String)
self.dateArray.append(object.createdAt)
self.tableView.reloadData()
//scroll to bottom
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.commentArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
}
}else {
print(error?.localizedDescription)
}
})
})
}
//Pagenation
func loadMore(){
//STEP 1. Count total comments in order to skip all except page size
let countQuery = PFQuery(className: "comments")
countQuery.whereKey("to", equalTo: commentUUID.last!)
countQuery.countObjectsInBackgroundWithBlock({(count:Int32, error:NSError?) -> Void in
//self refresher
self.refresher.endRefreshing()
//remove refresher if loaded all comments
if self.page >= count {
self.refresher.removeFromSuperview()
}
//STEP2. Load more comments
if self.page < count {
//increase page to laod 30 as first paging
self.page = self.page + 15
//request existing comments from the server
let query = PFQuery(className: "comments")
query.whereKey("to", equalTo: commentUUID.last!)
query.skip = count - self.page
query.addAscendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock({(objects:[PFObject]?, error:NSError?) -> Void in
if error==nil{
//clean up
self.usernameArray.removeAll(keepCapacity: false)
self.profileArray.removeAll(keepCapacity: false)
self.commentArray.removeAll(keepCapacity: false)
self.dateArray.removeAll(keepCapacity: false)
//find related objects
for object in objects! {
self.usernameArray.append(object.objectForKey("username") as! String)
self.profileArray.append(object.objectForKey("profileImg") as! PFFile)
self.commentArray.append(object.objectForKey("comments") as! String)
self.dateArray.append(object.createdAt)
self.tableView.reloadData()
}
}else {
print(error?.localizedDescription)
}
})
}
})
}
//Send Button Tapped
#IBAction func sendButtonTapped(sender: AnyObject) {
print("send tapped")
//STEP1. Add row in tableView
usernameArray.append(PFUser.currentUser()!.username!)
profileArray.append(PFUser.currentUser()?.objectForKey("profileImg") as! PFFile)
dateArray.append(NSDate())
commentArray.append(commentTextView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))
tableView.reloadData()
//STEP2. Send comment to server
let commentObj = PFObject(className: "comments")
commentObj["to"] = commentUUID.last
commentObj["username"] = PFUser.currentUser()?.username
commentObj["profileImg"] = PFUser.currentUser()?.valueForKey("profileImg")
commentObj["comment"] = commentTextView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
commentObj.saveEventually()
//Scroll to bottom
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: commentArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
//STEP 3. Reset UI
sendButton.enabled = false
commentTextView.text = ""
commentTextView.frame.size.height = commentHeight
commentTextView.frame.origin.y = sendButton.frame.origin.y
tableView.frame.size.height = self.tableViewHeight - self.keyboard.height - self.commentTextView.frame.size.height + self.commentHeight
}
//tableview
//cell numb
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return commentArray.count
}
//cell height
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
//Cell config
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//declaire cell
let cell = tableView.dequeueReusableCellWithIdentifier("commentCell") as! CommentTableViewCell
cell.usernameButton.setTitle(usernameArray[indexPath.row], forState: .Normal)
cell.usernameButton.sizeToFit()
cell.commentLabel.text = commentArray[indexPath.row]
profileArray[indexPath.row].getDataInBackgroundWithBlock({(data:NSData?, error:NSError?) -> Void in
cell.profileImagevView.image = UIImage(data: data!)
})
//calculate date
let from = dateArray[indexPath.row]
let now = NSDate()
let components : NSCalendarUnit = [.Second, .Minute, .Hour, .Day, .WeekOfMonth]
let difference = NSCalendar.currentCalendar().components(components, fromDate: from!, toDate: now, options: [])
if difference.second <= 0 {
cell.dateLabel.text = "now"
}
if difference.second > 0 && difference.minute == 0 {
cell.dateLabel.text = "\(difference.second)s"
}
if difference.minute > 0 && difference.hour == 0 {
cell.dateLabel.text = "\(difference.minute)m"
}
if difference.hour > 0 && difference.day == 0 {
cell.dateLabel.text = "\(difference.hour)h"
}
if difference.day > 0 && difference.weekOfMonth == 0 {
cell.dateLabel.text = "\(difference.day)d."
}
if difference.weekOfMonth > 0 {
cell.dateLabel.text = "\(difference.weekOfMonth)w."
}
cell.usernameButton.layer.setValue(indexPath, forKey: "index")
return cell
}
//clicked username button
#IBAction func usernameButtonTapped(sender: AnyObject) {
//call index of current button
let i = sender.layer.valueForKey("index") as! NSIndexPath
//Call cell to call further cell data
let cell = tableView.cellForRowAtIndexPath(i) as! CommentTableViewCell
//if user tapped on his username go home, else go guest
if cell.usernameButton.titleLabel?.text == PFUser.currentUser()?.username {
let home = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(home, animated: true)
}else {
guestname.append(cell.usernameButton.titleLabel!.text!)
let guest = self.storyboard?.instantiateViewControllerWithIdentifier("GuestHomeViewController") as! GuestHomeViewController
self.navigationController?.pushViewController(guest, animated: true)
}
}
//go back
func back(sender : UIBarButtonItem){
//push back
self.navigationController?.popViewControllerAnimated(true)
//clean comment uuid from holding information
if !commentUUID.isEmpty{
commentUUID.removeLast()
}
//clean comment owner from last holding information
if !commentOwner.isEmpty{
commentOwner.removeLast()
}
}
}
And It is my cell controller
import UIKit
class CommentTableViewCell: UITableViewCell {
#IBOutlet weak var profileImagevView: UIImageView!
#IBOutlet weak var usernameButton: UIButton!
#IBOutlet weak var commentLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
//default func
override func awakeFromNib() {
super.awakeFromNib()
//round profile
profileImagevView.layer.cornerRadius = profileImagevView.frame.size.width/2
profileImagevView.clipsToBounds = true
}
}
Two comments:
Use UIKeyboardDidShowNotification instead of UIKeyboardWillShowNotification
Do not modify frame directly when you're using auto layout. Just link your bottom layout constraint to the controller and change a constant value when needed.
Try this:
self.tableView.frame.size.height = self.view.frame.height - keyboard.height - self.tableView.frame.size.height
print("keyboard show")
self.commentTextView.frame = CGRect(self.commentTextView.frame.minX, self.commentTextView.frame.minY - keyboard.height, self.commentTextView.frame.width, self.commentTextView.frame.height)
self.sendButton.frame.origin.y = self.commentTextView.frame.origin.y
Edit
#Arsen's Answer makes much more sense and probably much easier, by the way :P But this should work the same.

How to disable gesture control in cvcalendar swift

I am using CVCalendar to display calendar . I want to disable the swipe action in the calendar view. how it is possible?
Following code which calls cvcalendar and displays calendar. I had two buttons and nex and prev to change month view.
import UIKit
class CalendarViewController: UIViewController {
#IBOutlet weak var menuView: CVCalendarMenuView!
#IBOutlet weak var calendarView: CVCalendarView!
#IBOutlet weak var monthLabel: UILabel!
#IBOutlet weak var eventTableView: UITableView!
var event : String = "1"
var shouldShowDaysOut = true
var animationFinished = true
override func viewDidLoad() {
super.viewDidLoad()
monthLabel.text = CVDate(date: NSDate()).globalDescription
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
calendarView.commitCalendarViewUpdate()
menuView.commitMenuViewUpdate()
}
}
/*
* CVCalendar library functions to display calender and events
*/
extension CalendarViewController: CVCalendarViewDelegate , MenuViewDelegate {
func presentationMode() -> CalendarMode {
return .MonthView
}
func firstWeekday() -> Weekday {
return .Sunday
}
func shouldShowWeekdaysOut() -> Bool {
return false
}
func didSelectDayView(dayView: CVCalendarDayView) {
let date = dayView.date
if dayView.isCurrentDay {
eventTableView.hidden = false
eventTableView.reloadData()
}
else {
eventTableView.hidden = true
}
println("\(calendarView.presentedDate.commonDescription) is selected!")
}
func presentedDateUpdated(date: CVDate) {
if monthLabel.text != date.globalDescription && self.animationFinished {
let updatedMonthLabel = UILabel()
updatedMonthLabel.textColor = monthLabel.textColor
updatedMonthLabel.font = monthLabel.font
updatedMonthLabel.textAlignment = .Center
updatedMonthLabel.text = date.globalDescription
updatedMonthLabel.sizeToFit()
updatedMonthLabel.alpha = 0
updatedMonthLabel.center = self.monthLabel.center
let offset = CGFloat(48)
updatedMonthLabel.transform = CGAffineTransformMakeTranslation(0, offset)
updatedMonthLabel.transform = CGAffineTransformMakeScale(1, 0.1)
UIView.animateWithDuration(0.35, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.animationFinished = false
self.monthLabel.transform = CGAffineTransformMakeTranslation(0, -offset)
self.monthLabel.transform = CGAffineTransformMakeScale(1, 0.1)
self.monthLabel.alpha = 0
updatedMonthLabel.alpha = 1
updatedMonthLabel.transform = CGAffineTransformIdentity
}) { _ in
self.animationFinished = true
self.monthLabel.frame = updatedMonthLabel.frame
self.monthLabel.text = updatedMonthLabel.text
self.monthLabel.transform = CGAffineTransformIdentity
self.monthLabel.alpha = 1
updatedMonthLabel.removeFromSuperview()
}
self.view.insertSubview(updatedMonthLabel, aboveSubview: self.monthLabel)
}
}
func topMarker(shouldDisplayOnDayView dayView: CVCalendarDayView) -> Bool {
return false
}
func dotMarker(shouldShowOnDayView dayView: CVCalendarDayView) -> Bool {
let day = dayView.date.day
if dayView.isCurrentDay{
return true
}
return false
}
func dotMarker(colorOnDayView dayView: CVCalendarDayView) -> [UIColor] {
let day = dayView.date.day
let color = UIColor.greenColor()
return [color]
}
func dotMarker(shouldMoveOnHighlightingOnDayView dayView: CVCalendarDayView) -> Bool {
return false
}
}
// MARK: - CVCalendarViewAppearanceDelegate
extension CalendarViewController: CVCalendarViewAppearanceDelegate {
func dayLabelPresentWeekdayInitallyBold() -> Bool {
return true
}
func spaceBetweenDayViews() -> CGFloat {
return 2
}
}
#IBAction func next(sender: AnyObject) {
calendarView.loadNextView()
}
#IBAction func previous(sender: AnyObject) {
calendarView.loadPreviousView()
}
You can disable scrolling of CVCalendarView this way:
Go to CVCalendarContentViewController.swift then find the extension CVCalendarContentViewController.
In that extension change the size of scrollView by replacing this line:
scrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height)
With this line of code:
scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height)
Hope it will help.
UPDATE:
Go to the project navigator the click on third icon which is Find Navigator as shown in below Image:
And search this line into that textField:
scrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height)
And your result will be like this:
Click on that result and replace that line with this line:
scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height)

Resources