BEMCheckBox don't seem to follow constraints - ios

Hi im trying to add a checkbox beside my task text but it wont follow my constraints, constraints set as pic one, but when I compile it shows me like in picture two.
I tried to put them inside a stackView but it made no difference.

The problem was I was setting the tableview.textfield.text = to the task text.
What I wanted to do was to set my customTableViewCells textfield.text = to the task.
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
and the put the cell.textFeild.text = myTask

Related

Custom Accessory Button in UITableViewCell

I am trying to add a custom button in the Accessory View for a UITableViewCell.
I have added the following code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ....
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "arrow-right-light.png")?.maskWithColor(color: .white), for: .normal)
button.addTarget(self, action: #selector(buttonTest), for: .touchUpInside)
button.tag = indexPath.row
cell.accessoryView = button
// ...
However, I don't see the button show up on in the Accessory View.
I can think of two possible reasons:
The problem might be that the button has no size, so it's invisible. Add this line, after setting the image:
button.sizeToFit()
Another possibility is that you have no image named "arrow-right-light.png". That wouldn't crash your existing code, but it would prevent the button from having any image so you wouldn't see it. Try saying UIImage(named: "arrow-right-light.png")! instead, just as a test; if you crash, that was indeed the problem, and then you can figure out what the correct name is.
I tried #matt's solution above, but this makes the button as large as the image.
I showed a iOS builtin info image which is smaller than the minimum advised button size of 44x44 points. Although the button works, it's hard(er) to tap; which is a common invisible UX issue seen in many apps (even professional ones.
Therefore instead of calling button.sizeToFit() you must set the buttons height (I did my full cell height of 50) and width to a nice & easy tappable 60.

How to add padding to searchbar swift ios

I'm trying to shift down my searchbar to align with the bottom of its parent cell in a collection view.
if(searchBarCellId == widgets[indexPath.item]){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchBarCellId, for: indexPath) as! SearchBarCell
cell.backgroundColor = UIColor.white
cell.searchBar.backgroundImage = UIColor.white.image()
cell.searchBar.tintColor = UIColor.gray
let textFieldInsideSearchBar = cell.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.lightGray
return cell
}
Is there a way to align the searchbar with the bottom cell of the cell programatically?
The custom xib editor does not allow me add constraints.
Updated with the full solution
Added constraint using editor
Edited codes:
if(searchBarCellId == widgets[indexPath.item]){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchBarCellId, for: indexPath) as! SearchBarCell
cell.backgroundColor = UIColor.white
let frameWidth = UIScreen.main.bounds.width
let frameHeight = UIScreen.main.bounds.width*0.2
cell.searchBar.frame=CGRect(x: 0, y: frameHeight*0.47, width: frameWidth*0.7, height: frameHeight*0.5)
cell.searchBar.backgroundImage = UIColor.white.image()
cell.searchBar.tintColor = UIColor.gray
let textFieldInsideSearchBar = cell.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.lightGray
return cell
}
InterfaceBuilder's .xib editor does indeed let you specify constraints. You need to turn on "Use Auto Layout" in the File Inspector (first tab in the right side pane).
Alternatively, you may set up constraints programmatically.
Edited for more info
How to add a bottom constraint to the search bar:
Select just the search bar. Then tap on the Add New Constraints button at bottom right of the .xib window (it looks like a tie fighter icon). Tap on the thin red line below the center box in the graphic, set the value to something appropriate (probably close to 0) and verify the view you're binding it to by pulling down the popup next to that value. Then tap the Add 1 Constraint button. That should bind the bottom of the search bar to the bottom of its parent view.
So the point is you just want to set up constraints for the search box relative to its immediate parent, which will be the view of the cell in your case, regardless that the xib doesn't know it's going to be for a cell.

How to avoid image size changes in UIImageView of UITableViewController in Swift

I am using a UITableViewController to display rows of playing cards. I've set the Mode to "Aspect Fit" and I've checked the "Clip Subviews" in the Storyboard for the ImageView in each row's cell, its ContentView parent, and the Cell that contains the ContentView.
Everything looks as expected when the table is initially displayed but, when I swipe to scroll through the table, some (not all) of the new rows that scroll into view have images scaled to the wrong size, as shown.
It seems like if I drag quickly, I get more rows that are of the wrong size. If I drag slowly, all the new rows are scaled appropriately. When I use a different emulator, the incorrect scaling would make some of the images too big rather than too small and so the entire row of cards would not fit within the display.
In the code, I've set the contentMode and clipToBounds, but they do not seem to help:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("LineCell", forIndexPath: indexPath)
let index = indexPath.row % 4
let imagename = "cards\(index)"
let im = UIImage(named: imagename)
cell.contentMode = UIViewContentMode.ScaleAspectFit
cell.clipsToBounds = true
cell.contentView.contentMode = UIViewContentMode.ScaleAspectFit
cell.contentView.clipsToBounds = true
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
cell.imageView?.clipsToBounds = true
cell.imageView?.image = im
return cell
}
Did I miss something obvious? Thanks!
I ended up using a programmatic way around the problem.
let cellImage : UIImageView = UIImageView(frame: CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height))
cellImage.contentMode = UIViewContentMode.ScaleAspectFit
cellImage.clipsToBounds = true
cellImage.image = im
cell.addSubview(cellImage)
I would still like to find a way to do it via the Storyboard, which I recall used to work in a straightforward manner...back before Apple started coming out with devices of all shapes and sizes leading to their need to offer increasingly complicated ways to accommodate all the sizes in a semi-automated, constraint-based system that requires quite a bit of overhead even for the simplest tasks. But, for now, this will do.

swift - set UILabel without reload of custom UITableViewCell

Is it possible to set a value for a UILabel in a custom UITableViewCell without reloading the cell?
I've tried many permutations of:
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell1
cell.customLabel.text = "text goes here"
.. and I don't understand why this won't work. I've got a UITextField in the UITableViewCell, and when I set the textField value I'd like only the label to update, but not reload the entire cell. Any suggestions?
cell.customLabel.setNeedsDisplay()
If you want to update the UIViews, you need to put them in main thread:
dispatch_async(dispatch_get_main_queue(),{
cell.customLabel.text = "text goes here"
})

Extra padding in UITableViewCell with multiple lines of text

Why is this happening? I'm not using IB by the way.
CODE:
cell = self.tableView.dequeueReusableCellWithIdentifier("NormalCell") as UITableViewCell
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = items[indexPath.row]["text"] as? String
IMAGES:
You need to set the constraints properly. Check out my screenshots below!
1) Set Constraints
http://prntscr.com/6r5i2z
2) Check Table Properties
http://prntscr.com/6r5i8y
3) Run the project! That's it!
Let me know, if you have any other issues!

Resources