UITableview First cell on weird position underneath UINavigationController - ios

The first cell of my tableView inside my UITableViewController starts just a bit in/underneath my UINavigationController. How can I fix this? I already tried:
self.tableView.contentInset = UIEdgeInsetsMake(63, 0, 0, 0) inside my viewDidLoad and viewDidAppear both did help a bit but only after scrolling..

Related

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.
Works for me (Swift 5):
class VerticallyCenteredTextView: UITextView {
override var contentSize: CGSize {
didSet {
var topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
topCorrection = max(0, topCorrection)
contentInset = UIEdgeInsets(top: topCorrection, left: 0, bottom: 0, right: 0)
}
}
}
The source - https://geek-is-stupid.github.io/2017-05-15-how-to-center-text-vertically-in-a-uitextview/

Tableview not scrolling on the first tap iOS

I have views hierarchy in Viewcontroller below. First time when I open the view controller my tableview is not scrolling at all even when I tap on tableview it will not recognize my tap. After that It is working fine as expected. I can able to scroll and select cell and working fine. Only first time I am having issue.
I don't have enough reputation so I can't post image.
ViewController -> View -> ScrollView -> View(viewSC) -> TableView -> TableViewCell
Anyone experiencing this kind of behavior.
Thanks in advance.
EDITED
self.scrollView.contentSize = self.viewSC.frameSize;
self.scrollView.contentInset = UIEdgeInsetsMake(self.HV.frameHeight, 0, 0, 0);
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(self.HV.frameHeight, 0, 0, 0);
this is the only code added in viewDidLoad. Other than that it is all working fine.
Are you trying to scroll as soon as the table view is created? When it comes to UITableView manipulation, you have to make sure that it's created and displayed before you try to scroll it. UITableView is picky like that. If you are loading your view and then you want to scroll the table view, call your code in viewDidAppear, not viewDidLoad.

Independent UIView in UITableView

I created an iPhone app where it came with the UINavigationController and UITableView predefined.
I want to add an UIView on the top of the screen but every time user scrolls tableView, my view also scrolls. Is there anyway to make the view independent from the tableView scroll?
Thanks
Don't use UITableViewController, use UIViewController and add UITableView as a subview
Create your view and set it as table header view of your table view.
UIView *yourView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[self.tableView setTableHeaderView:yourView];
It will not scroll with tableView but always remain on top of table view.

Setting the content inset of a UICollectionView inside of a UIPageViewController?

I have a UIPageViewController which contains UICollectionViewControllers who's collectionView'scontentInset that need to be adjusted. For various UI-layout reasons, I only need the contentInset on these UICollectionViewControllers when they appear in a UIPageViewController. Below is my attempt to adjust the contentInset for the collectionView.
self.viewControllers = [ ViewControllerA(), ViewControllerB(), ViewControllerC()]
for viewController in viewControllers {
if let vc = viewController as? UICollectionViewController, collectionView = vc.collectionView {
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: bottomContentInset(), right: 0)
}
}
The problem that I am encountering is that only the first view controller(on the first page) gets the contentInsetadjusted, is there a better way of doing this
Not sure if you solved this, but this may've been occurring because only the visible collection view's cells (in ViewControllerA) were dequeued. The collection views' cells in your other VCs possibly hadn't yet been dequeued, or it's possible the other VCs themselves hadn't yet been initialized (not sure how you instantiate the VCs). I would either use the UIPageViewController's delegate methods to set each collection view's contentInset property, or try it in the viewWillAppear: method of each VC.

Adding a UIView behind a UITableView causes tableview's content insets to be wrong aligned to topLayoutGuide

I have to show an UIMapView behind an UITableView into a controller in a way that the mapView becomes visible "behind" the tableview's header (similar to foursquare's "nearby places" view) with the typical parallax effect.
I've laid out my tableview within storyboards, added the mapView as a Container View behind the tableView, I've tried this in two ways: via storyboard and programmatically with something like:
[self.view addSubview: self.mapView];
[self.view sendSubviewToBack:self.mapView];
but in both cases the tableview's header ends under the navigation bar, I suppose by losing its reference to the toplayoutguide.
this is a screenshot of the view in storyboards:
and this is the result on the simulator:
As you can see, tableview's header starts under the navigation bar and not under the topLayoutGuide as it should.
Now, if I don't add any subview behind the tableview anything seems to work as expected, but I need the map behind so it is no use.
I've found a workaround with this code snippet placed into viewDidLayoutSubviews:
-(void)viewDidLayoutSubviews {
if ([self respondsToSelector:#selector(topLayoutGuide)]) {
UIEdgeInsets currentInsets = self.tableView.contentInset;
self.tableView.contentInset = (UIEdgeInsets){
.top = self.topLayoutGuide.length,
.bottom = currentInsets.bottom,
.left = currentInsets.left,
.right = currentInsets.right
};
}
}
But, it seems too much of a hack in first place, and also, tableview's contentInset starts at -64, not a big mess but I just don't like it.
My question so far is: why is this all happening and how can I fix it, if possible, in a more precise way?

Resources