Elements inside of the UITableView.backgroundView is not shown in UI Tests - ios

I have faced up with strange behavior of the accessibility with backgroundView of the UITableView. It is impossible to access elements in UI Tests that are inside viewContainer when it is backgroundView of the UITableView. They are even not shown in snapshot accessibility hierarchy.
I tried to set isAccessibilityElement flag of the container to false, then tried to put UI Elements into accessibilityElements array. It didn't help at all but UITableView have set accessibilityId that belongs to accessibilityId of the first accessibilityElement in container's array.
Then I tried to override methods from UIAccessibilityContainer. I put print into all methods to see the call sequence. But only method accessibilityElementCount() was called.
I tried the same code not for backgroundView but just for subview and it works. I think the problem is inside of the UITableView.backgroundView. I really confused of this behaviour

Related

UICollectionViewCell not visible but captured in view hierarchy

I have UICollectionView inside UITableViewCell. UICollectionViewCell's awakeFromNib is called. Cells are created and UICollectionViewCell's didSelect delegate works as per expectation. UICollectionView's scroll works as per expected. But cells are not visible. If view is captured in view hierarchy through debugging tool, cells are visible with proper data.
Thanks in advance.
As I had few wrong attributes mentioned in User Defined Runtime Attributes, view failed to render layer on screen with those attributes. This is the reason why cell were not visible on screen but captured on view hierarchy.
I got error on console because of wrong attribute on User Defined Runtime Attributes.
Failed to set (layer.shadowRadius) user defined inspected property on (UICollectionViewCell)
Fixing this issue solved my problem.
Thanks
did you check by setting color of UICollectionViewCell? you probably set color of UICollectionViewCell's color to white so it's not visible.
And if you set the color please let us know what you've done in your cellforitemanindexpath

UITextview text not centering vertically on initial view

I have an extension for UITextView which centers the text on a UITextView vertically that I found in the following SO answer: https://stackoverflow.com/a/38855122/4660602
My UITextView lives within a UITableViewCell, and the problem is that the function doesn't seem to work on the initial load. It only works when I reload the tableView or when I scroll.
I am calling the method within cellForRowAtIndexPath but I have tried adding it to my custom cell class in awakeFromNib and prepareForReuse but have and no luck. Wondering if anyone has any other advice / solutions.
EDIT:
Also forgot to mention, my VC with the tableView is embedded in a navigationBar and tabBar. When I switch to a new VC in the tabBar and then back, the UITextView text realigns to the top incorrectly, even if it was centered already.
Thanks!
The correct place for sizing code in a view is UIView.layoutSubviews. Since your centering function depends on the bounds, you have to call it in layoutSubviews, otherwise the bounds may not be correct (ie they match whats in the nib and not the current device). You can call setNeedsLayout in your cellforRowAtIndexPath to tell the view to update its layout after you ahve set the text.
In iOS 10, a bound of view hasn't initialized in viewDidLoad or awakeFromNib as before, so functions based on the view's bound don't work in viewDidLoad or awakeFromNib.
You should use it in viewDidAppear or when view's bound has certainly been initialized.

Custom view inside scroll view inside table cell is not responding to events

I have a UITableView in my view controller, and inside the cell there's a horizontal UIScrollView and inside this scroll view, I created many instances of a custom UIView (loaded from NIB file)
UITableViewCell
-- UIScrollView
-- MyView: UIView
Now MyView is not detecting touches, userInteractionEnabled is set to YES on every view in the hierarchy and I tried both ways
Implement touchesEnded: in MyView
Add a UITapGestureRecognizer to it
Both ways don't work, I guess it's something related to the fact that I have a UIScrollView inside the UITableViewCell
I am writing the application in Swift, not Objective-C, I don't think it matters since I guess it's a UIKit issue but who knows.
If you have any hint, I am all ears.
Thank you
Alternative for this case is add UITableViewCustomCell and add UICollectionView with horizontal scroll in Custom cell . It is more optimized way than adding UIView in scroll view because you can greatly speed things up. Instead of instantiating a lot of cells, you just instantiate as many as needed, i.e. as many that are visible (this is handled automatically). If scrolling to an area in the list where there are "cells" that haven't got their visual representation yet, instead of instantiating new ones, you reuse already existing ones.

UITableViewCell - setting background view and accessory in the initializer

I've browsed around a bit but didn't find any solution to my problem, so here I go:
I have a custom UITableViewCell subclass - MyCell. I am overriding the initWithReuseIdentifier to set stuff like label fonts, backgrounds, etc. However, when I try to set self.accessoryType or self.backgroundView = myImageView in the init, it doesn't seem to have any effect.
I have moved the bit of code where I am creating the image view and setting the backgrounView in the layoutSubviews method in my cell class. This however causes the problem that the background view appears as the topmost view in the cell, i.e. covering the labels and everything else. Any help would be greatly appreciated.
You many need to start moving your subviews around. See -sendSubviewToBack: for more detail.
Note: this will make things fragile and you may need to be very careful about your logic to avoid iOS version specific problems.
You may also want to read A Closer Look at Table View Cells for more information about the subviews of a UITableViewCell.

UIView updates when it is a tableHeaderView, but not when it is a subview

I have a custom UIView containing a TableView. Above the table I plan to display some other items. For now, I just want a button.
I am creating a UIView 'headerView', and adding the button to it. The button itself is just rigged to update it's title when it is pressed.
If I add the headerView to the table view as the tableHeaderView, it works just fine.
If I add the headerView above the tableview, as a sibling subview, it displays correctly but the button title does not update.
I have verified through logging that the method to update the button label is called, but the view seems to need a refresh.
Why would this be? Am I missing something simple.
Code to follow if it would help.
Note: I am not using IB for any of this.
Update:I have narrowed it down to one line. When I set the frame of the tableview, which I add below the headerView, I am setting the origin.y to the headerView.size.height. If I change this to use a hard coded number (even of the same value), the button updates.
It's hard to diagnose the issue without any code, but here are a few common issues/solutions that might help.
Use [button setTile:forState:] as opposed to button.titleLabel.text
Make sure you aren't missing any calls to layoutSubviews by calling setNeedsLayout and/or layoutIfNeeded on appropriate view.
Make sure you have the correct selector names hooked to the right target with the right control event.
Sounds like since it works in some cases, 1 and 3 aren't the issue. Also, since it works when you set the size differently it could be 2. The tableview would be calling layout methods correctly to it's header view, which might be why it works as the table header view and not as a normal sibling.

Resources