Posts

Post not yet marked as solved
6 Replies
1.6k Views
I am trying to develop an information based app with auto layout. Most of the pages are UITableViews, built in IB with static cells containing a simple text field. I would like the width of the text fields to automatically expand and the height to reduce if the device is rotated into landscape more, or if initially opened on a larger device.Using autolayout, I am seeing in most cases that the width does automatically expand to fill the horizontal space. However, the height does not seem to reduce, which leaves in some cases a lot of space - a large margin - beneath the text. First question, how do I avoid this?In some cases, the width does NOT expand across the screen, but an arbitrary margin seems to be set. This is sometimes applied to the entire text field within a table view cell, but in one case, the top two paragraphs extend across the screen, but the bottom paragraphs have a much larger right margin - which looks terrible.I am not sure how to add images here, so put them here: https://stackoverflow.com/questions/58410325/how-to-control-the-right-margin-of-my-uitextfield
Posted
by XofDyer.
Last updated
.
Post not yet marked as solved
2 Replies
549 Views
If I put a Text Field into a table view cell, change the text property from fixed to attributed, and then click inside the text in the property inspector - xcode crashes without saving the text field in the table view cell. This can be repeated.To work around this, I have to save the project immediately before clicking into the text field and crashing hte app. After reopening xcode, it works.Is this a known bug? or am I doing something that isn't intuitive?
Posted
by XofDyer.
Last updated
.
Post not yet marked as solved
6 Replies
4.4k Views
I've tried a few approaches to create a standard table - with a fixed number of columns and rows, but with variable column widths - for all device classes. The contraint system, even using Stack Views, created an impossible number of layout errors, and so it was suggested to use UICollectionViews, which appears better, but I cannot solve the following issues.Firstly, the cells in the last row of the table do not align perfectly with the cells above. Each cell is fractionally less width, making the final cell quite clearly unaligned. This issue, while minor in the example project, can appear larger depending on cell contents or device sizes.Secondly, while I can manage device orientation changes on most devices tested in the simulator, this not true for iPad Air devices. While the table layout is correct when I first run the project on iPad Air in any orientation, and is correct when re-orientated from landscape to horizontal; the layout breaks if I run the project on a iPad Air in portrait orientation, but then rotate to landscape.To demonstrate these issues, I've put the code below and a very simple project on github (link below). Within the project is a reference to the original source for setting column widths on StackExchange. It would be great if someone with a greater level of undestanding could provide a definitive method, for all devices and in all orientations, to create tables with a fixed number of columns that also have variable widths.TableRowColumn.zip func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { var columnPortion = CGFloat() let columnNumber = indexPath.item % 5 switch columnNumber { case 0: columnPortion = 0.6 case 1: columnPortion = 0.3 case 2: columnPortion = 1 case 3: columnPortion = 1.4 case 4: columnPortion = 1.7 default: columnPortion = 1 } // https://stackoverflow.com/questions/54915227/uicollectionview-remove-space-between-cells-with-7-items-per-row var cellWidth = CGFloat() let availableWidth = collectionView.bounds.size.width print("available width", availableWidth) let minimumWidth = floor(availableWidth / numberOfColumns) print("minmum width", minimumWidth) cellWidth = minimumWidth * columnPortion - 1 // the - 1 is to remove problems with rounding print("cell width", cellWidth) return CGSize(width: cellWidth, height: rowHeight).xx_rounded() }
Posted
by XofDyer.
Last updated
.
Post marked as solved
11 Replies
2.2k Views
I've a grid table using a UICollectionView - 4 columns x 4 rows. In the top row, I've three UIPickerViews. If the user selects a new value in the picker, the UILabels in the pickers revert to previous label (columns 1 and 4, not 3), and change the label.text on other pickers.In columns 1 and 4, while the selection calls the delegate method, the UILabel rolls back to the previous value; and the label of the other picker (col. 1 or 4 - NOT 3) updates to the value selected in the selected picker (without changing the values in the not selected column).In column 3, the picker appears to work perfectly - delegate called correctly and UILabel remains on the selected value. But if one or both of the labels in columns 1 or 4 have been changed from the default or first value in array, then these labels (in col.s 1 and 4) exchange values (when the picker in col. 3 is changed).Within a custom UITableViewCell, managing a UICollectionView, I instantiate my custom UICollectionViewCell with a picker in it:func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseI4CVCell, for: indexPath) as! CVCell switch indexPath.item { case 0, 2, 3: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseI4PickerCVCell, for: indexPath) as! PickerCVCell cell.caseOfPicker = indexPath.item cell.reference = self return cellclass PickerCVCell: UICollectionViewCell, UIPickerViewDelegate, UIPickerViewDataSource { var reference: EgPickerDelegate? var caseOfPicker: Int? private var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>? // controller for fetched results @IBOutlet weak var pickerInCVCell: UIPickerView! override func awakeFromNib() { super.awakeFromNib() configureFetchedResultsController() pickerInCVCell.delegate = self pickerInCVCell.dataSource = self } private func configureFetchedResultsController() { guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Gender") let sortDescriptor = NSSortDescriptor(key: "sortOrder", ascending: true) fetchRequest.sortDescriptors = [sortDescriptor] fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: appDelegate.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil) fetchedResultsController?.delegate = self as? NSFetchedResultsControllerDelegate do { try fetchedResultsController?.performFetch() } catch { print(error.localizedDescription) } } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if let objects = fetchedResultsController?.fetchedObjects { return objects.count } return 4 } func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { var pickerLabel: UILabel? = (view as? UILabel) let path = NSIndexPath(item: row, section: 0) if let gender = fetchedResultsController?.object(at: path as IndexPath) as? Gender { let text = gender.text ?? "Text not found" if pickerLabel == nil { pickerLabel = UILabel() pickerLabel?.textColor = UIColor.black pickerLabel?.textAlignment = NSTextAlignment.center pickerLabel?.font = pickerLabel?.font.with( .traitBold, .traitItalic) } pickerLabel?.text = text } return pickerLabel! } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { reference?.myPickerDidSelectRowInCase(selectedRowValue: row, caseOfPicker: caseOfPicker) } func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return CGFloat(120.0) } }The custom collectionView pickerView didSelectRow delegate method passes the correct caseOfPicker value after selection.I've tried instantiating cells in different ways - to distinguish them as objects. I've tried using pickerView titleForRow instead of viewForRow, but it behaves the same way. Can't find a helpful dataSource method.
Posted
by XofDyer.
Last updated
.