After added below code its display list of UILabel below FSCalendar dates.
But the label overlap dates inside calendar.
How to align weekly / Monthly FSCalendar Label below dates?
func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at monthPosition: FSCalendarMonthPosition) {
let labelMy2 = UILabel(frame: CGRect(x: 10, y: calendar.rowHeight, width: cell.bounds.width - 20, height: 20))
labelMy2.font = UIFont.init(font: Font.interRegular, size: 10)
labelMy2.text = " 00.00 "
labelMy2.textAlignment = .center
labelMy2.backgroundColor = UIColor.init(named: ColorPalette.gray100Color)
labelMy2.textColor = UIColor.init(named: ColorPalette.gray600kColor)
cell.addSubview(labelMy2)
}
Below Added Image
Related
There seems to be some wasted space towards the right of the date picker component:
And it doesn't matter if I increase or decrease the value of width/height of its frame; it stays the standard size.
let datePicker = UIDatePicker(frame: CGRect(x: 20, y: 100, width: 500, height: 200))
datePicker.datePickerMode = .date
view.addSubview(datePicker)
My question is, can it be adjusted?
edit: So I managed to find its short form but thats only addable through the storyboard.
Heres the view:
The top first picker is added programmatically and the second via storyboard. For some reason, I'm unable to make the programmatically created picker smaller...
You can just ask the date picker which size is sufficient ;)
Option A
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
// Let it size itself to its preferred size
datePicker.sizeToFit()
// Set the frame without changing the size
datePicker.frame = .init(x: 20, y: 100, width: datePicker.bounds.size.width, height: datePicker.bounds.size.height)
view.addSubview(datePicker)
Option B
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
// Ask the picker for its preferred size
let datePickerPreferredSize = datePicker.systemLayoutSizeFitting(view.bounds.size)
// Set the frame using that size
datePicker.frame = .init(x: 20, y: 100, width: datePickerPreferredSize.width, height: datePickerPreferredSize.height)
view.addSubview(datePicker)
My requirement is, I need to populate 10 cell's in collection view controller and I need to display in 6Th cell battery status and in 7th need to switch. The remaining cell is not required to show the battery status and switch. I am adding these two (battery status & switch) through programmatically.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if indexPath.row == 6 {
let percentage: CGFloat = 0.4
let imageview:UIImageView=UIImageView(frame: CGRect(x: 10, y: 50, width: 30, height: 18))
let img : UIImage = UIImage(named:"batteryIcon")!
imageview.image = img
let batteryView = UIView.init(frame: CGRect(x: 10, y: 50, width: 30, height: 18))
cell.addSubview(batteryView)
let colorRect = CGRect(x: batteryView.bounds.origin.x, y: batteryView.bounds.origin.y, width: batteryView.bounds.width * percentage, height: batteryView.bounds.height)
let coloredView = UIView(frame: colorRect)
coloredView.backgroundColor = UIColor.green
batteryView.addSubview(coloredView)
cell.contentView.addSubview(imageview)
} else if indexPath.row == 7 {
let switchDemo = UISwitch(frame: CGRect(x: 10, y: 40, width: 37, height: 18))
switchDemo.isOn = true
switchDemo.setOn(true, animated: false)
switchDemo.transform = CGAffineTransform(scaleX: 0.70, y: 0.70)
switchDemo.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)
cell.contentView.addSubview(switchDemo)
}
return cell
}
Here it is Working fine. But the problem is when I call "collectionView.reloadData()" Instead of showing battery status and switch 6Th and 7Th cells, Those two appearing in other cells also.
I am not getting, What mistake I am done.
For a better understanding of my requirement, I am sharing an image.
I am getting the widgets/data overlapped in every cell when I scroll down or up my tableview. What I am doing is, I have an array in elements are wCheckinArray = ["Date", "TextFeild", "TextView", "Check", "Sign"]. Now I am checking if there is an element "anyElement" in wCheckinArray then I am making an anyElement in a cell and add as a subview in content view of a cell.
Example:
Under cellForRowAtIndexPath method of UITableView :
let w = wCheckinArray[indexPath.row]
if w.contains(Date) {
// make a date picker and add it as a subview to content view of cell
}
// and so on
Real code:
var Cell : UITableViewCell?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellforrowatindexpathruns")
var instockSignView : Int = 1
Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell
if wCheckinArray.count == 0 && lCheckinArray.isEmpty {
print("no textFields in Checkin")
}
else
{
print("widget process i in winstock loop")
let w = wCheckinArray[indexPath.row]
if w.contains(wTextField) {
print("textField was placed")
newLabel = UITextField(frame: CGRect(x: 10, y: 10, width: 250, height: 30))
newLabel.textColor = UIColor.gray
newLabel.tag = widgetTagOk
print("You have a instock textLabel with tag \(newLabel.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
newLabel.text = " "
} else {
newLabel.placeholder = lCheckinArray[indexPath.row]
newLabel.layer.cornerRadius = 3
newLabel.layer.borderColor = UIColor.gray.cgColor
newLabel.layer.borderWidth = 1
//instockTextField += 1
widgetTagOk += 1
print("\(wCheckinArray.count)")
Cell?.contentView.addSubview(newLabel)
}
}
if w.contains(wTextView) {
print("textView was placed")
newTextView = UITextView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newTextView.textAlignment = NSTextAlignment.justified
newTextView.textColor = UIColor.black
newTextView.backgroundColor = UIColor.white
newTextView.font = UIFont.systemFont(ofSize: 15)
newTextView.tag = widgetTagOk //instockTextView
print("You have a instock textView with tag \(newTextView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the TextView place text \(lCheckinArray[indexPath.row])")
newTextView.layer.cornerRadius = 3
newTextView.layer.borderColor = UIColor.gray.cgColor
newTextView.layer.borderWidth = 1
widgetTagOk += 1 //instockTextView += 1
print("\(wCheckinArray.count)")
Cell?.contentView.addSubview(newTextView)
Cell?.contentView.addSubview(textViewLabel)
}
}
if w.contains(wSign) { //wInstockArray.contains(wSign) {
print("sign was place d")
newSignView = UIView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newSignView.backgroundColor = UIColor.white
newSignView.tag = instockSignView
print("You have a instock signView with tag \(newSignView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
print("signview can nit be added as a widget")
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the TextView place text \(lCheckinArray[indexPath.row])")
newSignView.layer.cornerRadius = 3
newSignView.layer.borderColor = UIColor.gray.cgColor
newSignView.layer.borderWidth = 1
instockSignView += 1
print("\(wCheckinArray.count)")
print("signview has added as a instock widget")
Cell?.contentView.addSubview(newSignView)
Cell?.contentView.addSubview(textViewLabel)
}
}
if w.contains(wDate) { //wInstockArray.contains(wSign) {
print("date was placed")
newDatePicker = UIDatePicker(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newDatePicker.backgroundColor = UIColor.white
newDatePicker.tag = instockSignView
print("You have a checkin date picker with tag \(newSignView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
print("date picker can nit be added as a widget")
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the date picker place text \(lCheckinArray[indexPath.row])")
newDatePicker.layer.cornerRadius = 3
newDatePicker.layer.borderColor = UIColor.gray.cgColor
newDatePicker.layer.borderWidth = 1
instockSignView += 1
print("\(wCheckinArray.count)")
print("date picker has added as a checkin widget")
Cell?.contentView.addSubview(newDatePicker)
Cell?.contentView.addSubview(textViewLabel)
}
}
// } //for i in instockarr comment bracket
print("could not add widgets")
print("here is w id \(idCheckinArray),here is w name: \(wCheckinArray), here is w data \(lCheckinArray)")
}
return Cell!
}
This is how tableView looks when I scroll up or down the tableView
What I have Googled
What I have googled is This question which suggests to me that I should check the clear graphics context which is done by default in Xcode
Also searched Here which suggest me to change the cell identifier for every cell, well how can I do? How can I change the cell identifier which I have mentioned in Tableview cell's attributes inspector in Xcode. Is there any solution to reuse this programatically.
Update
The tags of widget like textview.tag or textfield.tag are increasing when I scroll the tableview from bottom to upward here is how I printed the tag of textfield
You better remove your all subviews creating subview.And make sure your y position to every elements is not same
Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell
for subView in cell.contentView.subviews{
subView .removeFromSuperview()
}
I am trying to set the time for UIDatePicker from the text of UITextField. But it always gets reset to current time. Text on UITextField is 14:01 and this is how I am trying to set time on UIDatePicker :
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
let time = formatter.date(from: textField.text!)
datePickerTime.date = time!
This is is the initialisation of datePickerTime :
datePickerTime = UIDatePicker(frame: CGRect(x: CGFloat(0), y: CGFloat(210), width: CGFloat(320), height: CGFloat(216)))
datePickerTime.datePickerMode = .time
datePickerTime.minimumDate = Date()
I have tried similar case with setting date(not time) and it works fine.
I have a UITableView that needs to support content by listing style something like
But the tricky part is that the amount of "Label" will vary with each cell, some may have only 4 but also up to 12. Also the "Value" can be either a single word or short phrase up to two lines(like in the image above). So I decided to use UIStackView to help me pack and size my UILabels used to display this. I am currently stuck at a problem when the "Label" varies in length like:
I need the leading end of the "Values" to be aligned like the first image even though the "Label" vary in length. Is there a behaviour of UIStackView that allows so? Or is there another approach that can allow me to obtain the results I need?
Note: Each "Label" and "Value" is in one UIStackView, I did it to align them.
I tried using String Formatting too, but "Values" with more than one line will wrap under the label instead of wrapping by itself like I manage to do in the images.
I tried placing all "Labels" in one UIStackView and all "Values" in another, I could no get them to align like they do in the images once the "Value" is more than one line.
Or if it might be a mistake I made somewhere, this is how I created the UIStackViews:
var higherCount = 0
if labels.count<values.count {
higherCount = values.count
} else {
higherCount = labels.count
}
mainStackView = UIStackView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
for i in 0..<higherCount {
var height:CGFloat = 20
if (values[i] as NSString).size(attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: UIFont.systemFontSize)]).width > frame.width {
height = 42
}
let stackViewToAdd = UIStackView(frame: CGRect(x: 0, y: 0, width: frame.width, height: height))
let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: height))
if labels.count<higherCount {
label.text = ""
} else {
label.text = labels[i]
}
label.setContentCompressionResistancePriority(1000, for: .horizontal)
label.setContentHuggingPriority(999, for: .horizontal)
stackViewToAdd.addArrangedSubview(label)
let value = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: height))
if values.count<higherCount {
value.text = ""
} else {
value.text = values[i]
}
value.lineBreakMode = .byWordWrapping
value.numberOfLines = 2
stackViewToAdd.addArrangedSubview(value)
mainStackView?.addArrangedSubview(stackViewToAdd)
}
mainStackView?.alignment = .fill
mainStackView?.axis = .vertical
mainStackView?.distribution = .fill
I you created your cell programmatically, then you can resize the cell programmatically depends on the size of UILabel Content.
In my case Label font is UIFont.systemFont(ofSize: 15), minimum TableViewCell height is 50, arrLabel1 and arrLabel2 will be the content of Labels.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrLable1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "\(indexPath.row)")
let lable1 = UILabel(frame: CGRect(x: 10, y: 10, width: view.frame.size.width - 20, height: 0))
lable1.numberOfLines = 0
lable1.font = UIFont.systemFont(ofSize: 15)
lable1.text = arrLable1[indexPath.row]
lable1.sizeToFit()
cell.addSubview(lable1)
let lable2 = UILabel(frame: CGRect(x: 10, y: lable1.frame.origin.y + lable1.frame.size.height + 10 , width: view.frame.size.width - 20, height: 0))
lable2.numberOfLines = 0
lable2.font = UIFont.systemFont(ofSize: 15)
lable2.text = arrLable2[indexPath.row]
lable2.sizeToFit()
cell.addSubview(lable2)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
-> CGFloat {
let boundingRect1 = arrLable1[indexPath.row].boundingRect(with: CGSize(width: view.frame.size.width - 40 , height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin,attributes:[ NSFontAttributeName : UIFont.systemFont(ofSize: 15)] ,context: nil)
let boundingRect2 = arrLable2[indexPath.row].boundingRect(with: CGSize(width: view.frame.size.width - 40 , height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin,attributes:[ NSFontAttributeName : UIFont.systemFont(ofSize: 15)] ,context: nil)
guard boundingRect1.height + boundingRect2.height + 30 > 50 else {
return 50
}
return boundingRect1.height + boundingRect2.height + 30
}
Set Label numberOfLines to 0