Xcode - Blank Space After UITableView - xcode

What is the best/correct way to get a UITV Cells to resize based on the display they're being viewed on? Basically, I would like the same number of rows displayed on screen, regardless of the device being used. Is this done in code or am i missing something in the storyboard?
Please see my example below (this is what i see in storyboard):
This is I see at the bottom of the screen on an iPhone 6 simulator:
This is what I see at the bottom of the screen on an iPhone 4s simulator:
I'm pretty new to Xcode so appreciate this is probably a well documented question... but i've been at it for a while now and i'm at the point where i need a little help -- so any help appreciated.
Thanks in advance.

You will have to handle this in your UITableViewController subclass, there isn't a way to do what you want to do in the storyboard.
Assuming the following:
The table view is fixed in portrait mode
The table view has only one section
Adding this method to your UITableViewController subclass will return a dynamic height based on the height of the table view:
(Swift)
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
let numCells = self.tableView(self.tableView, numberOfRowsInSection:0)
let cellHeight = self.tableView.frame.size.height / CGFloat(numCells)
return cellHeight
}
(Objective-C)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger numCells = [self tableView:self.tableView numberOfRowsInSection:0];
CGFloat cellHeight = self.tableView.frame.size.height / numCells;
return cellHeight;
}

Related

In a view-based NSTableView, the NSTextField is clipped by an NSImageView

TL;DR:
In macOS 10.11, a view based NSTableView containing an NSTextField and an NSImageView right under the textfield, with some rows having an image and others not, some texts are clipped after having scrolled the table down then up.
Before scrolling:
After scrolling:
When I say "clipped" it means that the text view is at its minimum height as defined with autolayout, when it should be expanded instead.
Note that this faulty behavior only happens in macOS 10.11 Mavericks. There's no such issues with macOS 10.12 Sierra.
Context
macOS 10.11+, view based NSTableView.
Each row has a textfield, and an image view just below the textfield.
All elements have autolayout constraints set in IB.
Goal
The text view has to adapt its height vertically - the image view should move accordingly, staying glued to the bottom of the textfield.
Of course the row itself also has to adapt its height.
Sometimes there's no image to display, in this case the image view should not be visible.
This is how it's supposed to render when it works properly:
Current implementation
The row is a subclass of NSTableCellView.
In the cell the textfield is set with an attributed string.
In tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat, I make a dummy textView that I use to find the actual textfield's height, and return an amended row height accordingly. I also check if there's an image to display and if it's the case I add the image height to the row height.
let ns = NSTextField(frame: NSRect(x: 0, y: 0, width: defaultWidth, height: 0))
ns.attributedStringValue = // the attributed string
var h = ns.attributedStringValue.boundingRect(with: ns.bounds.size, options: [.usesLineFragmentOrigin, .usesFontLeading]).height
if post.hasImage {
h += image.height
}
return h + margins
In tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? I prepare the actual cell contents:
getUserAvatar { img in
DispatchQueue.main.async {
cell.iconBackground.layer?.backgroundColor = self.prefs.colors.iconBackground.cgColor
cell.iconBackground.rounded(amount: 6)
cell.iconView.rounded(amount: 6)
cell.iconView.image = img
}
}
cell.usernameLabel.attributedStringValue = xxx
cell.dateLabel.attributedStringValue = xxx
// the textView at the top of the cell
cell.textLabel.attributedStringValue = xxx
// the imageView right under the textView
if post.hasImage {
getImage { img in
DispatchQueue.main.async {
cell.postImage.image = img
}
}
}
Issues
When scrolling the tableView, there's display issues as soon as one or several rows have an image in the image view.
Clipped text
Sometimes the text is clipped, probably because the empty image view is masking the bottom part of the text:
Normal
Clipped
IMPORTANT: resizing the table triggers a redraw and fixes the display issue...
What I've tried
Cell reuse
I thought the main issue was because of cell reuse by the tableView.
So I'm hiding the image field by default in tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?, only unhiding if there's an image to set, and I do it on the main thread:
guard let cell = tableView.make(withIdentifier: "xxx", owner: self) as? PostTableCellView else {
return nil
}
DispatchQueue.main.async {
cell.postImage.isHidden = true
cell.postImage.image = nil
}
// set the text view here, the buttons, labels, etc, then this async part runs:
downloadImage { img in
DispatchQueue.main.async {
cell.postImage.isHidden = false
cell.postImage.image = img
}
}
// more setup
return cell
But the imageView is still blocking the text view in some cases.
And anyway, sometimes the text is clipped at the first display, before any scrolling is done...
CoreAnimation Layer
I thought maybe the cells need to be layer backed so that they're correctly redisplayed, so I've enabled CoreAnimation layer on the scrollView, the tableView, the tableCell, etc. But I hardly see any difference.
A/B test
If I remove the imageView completely and only deal with the textfield everything works ok. Having the imageView is definitely what is causing issues here.
Autolayout
I've tried to handle the display of the row contents without using autolayout but to no avail. I've also tried to set different constraints but clearly I suck at autolayout and didn't manage to find a good alternative to my current constraints.
Alternative: NSAttributedString with image
I've tried to remove the image view and have the image added at the end of the attributed string in the text view instead. But the result is often ugly when it works - and for most times it just doesn't work (for example when the image can't be downloaded in time to be added to the attributed string, leaving the cell without text or image at all and at a wrong height).
Question
What am I doing wrong? How could I fix these issues? My guess is that I should change the autolayout constraints but I don't see a working solution.
Or maybe would you do this entirely differently?
Text field has "Preferred Width" field in IB which allows tune up correlation of intrinsic size with auto-layout calculated size.
Changing this IB property value to "First Runtime Layout Width" or "Explicit" often helps resolving similar issues.
This resolved a lot of my issues in past.

How do I remove the focus shadows from a UIView in a UICollectionView in swift?

I'm working on a tvOS project in swift and have a horizontally aligned UICollectionView where each UICollectionViewCell is a custom UIView consisting of a UIImageView and a Label below it. So my UICollectionView looks like this:
| UIImageView | | UIImageView |
| Label | | Label |
I also wanted the image to grow/shrink as the particular view gained and lost focus. In my collection view I added the code
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
And in my custom cell class I have the line
imageView.adjustsImageWhenAncestorFocused = true;
So this somewhat works, although with 2 problems.
It seems all the views in the collection have a weird shadow under them, even when they're not focused. And then when they are focused, the image grows as expected, but it has another shadow under it. You can see what I mean here:
The first item in the collection has the focus
How do I get rid of them? I've tried all of these but none of them seem to work
contentView.layer.shadowOpacity = 0.0 // My imageView is a subview of this
contentView.layer.shadowColor = UIColor.clearColor().CGColor
imageView.layer.shadowOpacity = 0.0
imageView.layer.shadowColor = UIColor.clearColor().CGColor
It's not possible right now. I forwarded this question to Apple, and the response was: https://forums.developer.apple.com/message/105045#105045
I found a solution for UITableViewCell and CollectionViewCell:
self.clipToBounds = true
self.layer.cornerRadius = 10
This is if you needed to restore rounded edges.

Set NSTextField frame height in tableView viewForTableColumn not working

I have a .xib with 2-3 NSTextFields displayed in it (as shown below). I would like to resize one of these NSTextFields so it best fits the content it is displaying. My problem is that I cannot change the size of the Text Field in the viewForTableColumn at all.
I have successfully set my heightOfRow. That is working nicely.
I have turned off AutoLayout for the field as it wasn't quite working properly for me.
Now in tableView viewForTableColumn I have the following code:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("MessageView", owner: self) as! MessageView!
var newFrame = cell.messageSubject.frame
newFrame.size.height = 200
cell.messageSubject.frame = newFrame
return cell
}
As you can see the frame for the Subject field remains unaltered.
Interestingly the size of the frame is always the default size set in the Interface builder even when AutoLayout is turned on.
My conclusion is that I'm trying to do this in the wrong method.
I have been able to resize the frame in a simple non-table based viewController with no problem.

Xcode custom UITableViewCell has unwanted image indentation after selection

Using Xcode 6.3.1 and iOS 8.3 (and Swift 1.2 for what it matters here) I created a custom UITableViewCell that includes an UIImageView, and two UILabels. The whole thing has been formatted using Auto-Layout. No errors, no warnings. When I run the app, all goes fine until... I selected a table row. In that case the image indents a little (simply selection, no editing enabled) and remains indented.
See the first row of the image to get an impression:
Hiding the image removes the problem, and setting the UITableViewCell's Selection parameter (in Xcode) to None also removes the problem (but that feels unnatural when selecting a row).
I am not sure whether it is Auto Layout related as it only appears after selecting the row, but I do not know how to fix it properly. Any ideas?
Edit: here is my cell code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(customCellIdentifier, forIndexPath: indexPath) as! MenuTableViewCell
let item = menuItems[indexPath.row]
cell.titleLabel!.text = item.title
cell.subtitleLabel?.text = item.subTitle
cell.imageView?.image = UIImage(named: item.icon)
return cell
}

UITableViewAutomaticDimension not working in Xcode 6.3

I updated my Xcode to 6.3 along with Swift 1.2, and I made the transition.
Everything works except dynamic row height for table view. I have those on 3 completely different table views, so it probably isn't something else influencing the bug.
I set all of the table views to:
tableView.rowHeight = UITableViewAutomaticDimension
and all of my xib files are properly constrained.
Any ideas?
I simply added UITableViewAutomaticDimension to estimatedrowforindexpath as well. You can see how it works on the BEAM App (under 'people' category).
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Estimated row height needs to be provided.
It is better not to implement estimatedHeightForRowAtIndexPath: unless necessary.
estimatedRowHeight is cheaper provided you can come up with a value
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30 //Provide any appropriate value

Resources