Don't use SafeArea inset on iPhone X for first UITableViewCell - ios

I have a UITableView and the first row is used as a sort of header cell with a full bleed background image and other elements. For this cell I do NOT want to use the SafeArea and I want the UIView to expand all the way to the edge of the screen. Currently I get this:
I have tried to set this manually for the cell:
if (#available(iOS 11.0, *)) {
headerCell.insetsLayoutMarginsFromSafeArea = NO;
headerCell.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0);
UIEdgeInsets i = headerCell.layoutMargins;
NSLog(#"Left: %f", i.left);
}
Sadly this doesn't work.
Here is a mockup of what it should look like, with the first cell contents NOT being affected by the safe area:
Is there any way to do what I am wanting just for the first cell?

I found that I needed to uncheck the Content View Insets To Safe Area checkbox that is on the UITableView.
Doing this fixed the issue!

Marking the "Content insets" under Size Inspector to "Never" worked for me.

In code, it could be accomplished by
tableView.insetsContentViewsToSafeArea = false

Just go to the tableview Size inspector and look for the Content Insets by default it will be set to Automatic change that to Never as shown like in the image below

Related

UITableViewCell contentView inset on iPhone X

Any UITableViewCell created on iPhone X has a 44 points inset for content view. This means width of contentView of UITableViewCell is by default 44pts less than the cell width. I do not need to have this behavior. How do I get contentView width same as cell width (or like it is there on other iPhone models)?
Update: I solved the problem by setting tableView.insetsContentViewsToSafeArea = NO. Now working on same issue for other elements such as UINavigationBar items. Unable to find an equivalent was for UIBarButtonItem for navigation bar. Any insight is appreciated.
Maybe you could use additionalSafeAreaInsets on your view controller, it's there to reduce the actual usable space but with negative Insets it should do the opposite.
https://developer.apple.com/documentation/uikit/uiviewcontroller/2902284-additionalsafeareainsets
In your table view set insetsContentViewsToSafeArea to false.
tableview.insetsContentViewsToSafeArea = false

How to remove UITableView top inset when embedded in ContainerView?

I am currently working on an iPhone App. I now have a problem with the top spacing inset of a UITableView. See this screenshot:
There should be no space between the table view cells and the buttons.
I do not know how to fix this. The UITableView is embedded in a ContainerView like this:
I think I got the container view constraints right. Top Space to Chapter Button is set to 0.
I tried to change some settings of the table view controller in storyboard. For example the Adjust Scroll View Insets. However it does not change anything when I disable that.
I also tried to set the TableView insets directly in the code in viewDidLoad():
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
However this also did not fix it.
Can anyone help? I have no idea where to look.
This can be solved by disabling auto adjustment of scroll view insets. However this setting has to be applied to the ViewController which contains the ContainerView (with the UITableView inside of it). It does not work when applied to the UITableView itself.
This can be done via Storyboard:
Or via code in viewDidLoad(): self.automaticallyAdjustsScrollViewInsets = false
Since iOS 15 there's a new parameter which can introduce a top gap when using a section header. This can be removed with the following:
if #available(iOS 15, *) {
tableView.sectionHeaderTopPadding = 0
}
Try this Hope this work.
hide View which contains the ContainerView.
self.automaticallyAdjustsScrollViewInsets = NO

How to remove top padding for text in UITextView Swift

I have a unusual problem. First i have Nav Controller and as a one page i have view controller with top panel for back button. I added uiview and uitextview on it. Then i make all needed constraints and after run and see top padding for text in textview about ~60 px . And i dont really know how to remove it.
I tried:
textViewUserInput.contentInset = UIEdgeInsetsMake(-50, 5, 5, 5);
it works but value -50 for top inset is not good for different devices and orientation
I also tried:
textViewUserInput.textContainerInset = UIEdgeInsetsZero;
textViewUserInput.textContainer.lineFragmentPadding = 0;
doesn't change anything
In Main.storyboard, select your view controller the one that has the UITextView in and underneath your attributes inspector tab uncheck "Adjust scroll view insets". I had the same problem and that worked for me.
Or you can add [textView setTextContainerInset:UIEdgeInsetsZero]; or textView.textContainerInset = .zero in your code.

UICollectionView working on iOS7 but not on iOS6

My UICollectionView cells don't get displayed on iOS6, because my delegate method cellForItemAtIndexPath doesn't get called. I suspect because of this warning:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the `UICollectionView`
minus the section inset's top and bottom values.
I don't get the warning on iOS7, and all the cells display correctly there too.
I've set my collectionView frame to height 270 in the .xib and there are no insets defined.
I've set my cell height to 270 in the .xib.
I can print out my collectionView frame at runtime and it's 271.
Also, my collectionview is actually inside a custom tableview cell.
Any ideas?
Thanks!
Try to set self.automaticallyAdjustsScrollViewInsets = NO
This was introduced in ios7 so you might want to wrap that with an ios version check, if you are supporting ios6 and below.
This fixed my problem! In my .xib, setting my Collection View Size Cell Size to a smaller value.
My setup is that I have this collectionview inside a custom tableview cell and
I do return the height of my tableview cell programatically (depending on the content). So it could be that my warnings had to do with my collectionview not fitting inside the tableview cell. So setting the initial collectionview to a smaller value fixed it.
I was on the wrong path thinking that the problem was with my collectionview and its colletionview cell.
Maybe?
self.automaticallyAdjustsScrollViewInsets = NO
actually did the trick. it also resolved my issue in swift, where the cells of a horizontal flow layout had a frame top of -32 (???) and did not fit into the collection view properly.
I found that I had to manually set self.collectionView.collectionViewLayout.itemSize in viewWillLayoutSubviews.
- (void)viewWillLayoutSubviews {
self.collectionView.collectionViewLayout.itemSize = CGRectMake(...);
}
Another possibility to generate the same trick would be to implement the method
collectionView:layout:sizeForItemAtIndexPath:
I have the same issue, in my case the size of collectionCell in storyboard is 96x96 and also under -(CGSize)collectionView:layout:sizeForItemAtIndexPath:
the solution was removing this delegate:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets insets = {.left = 10, .right = 10, .top = 5, .bottom = 5};
return insets;
}
And by the way this is under ios7, it's late but hope this will help some.. Cheers..
Set:
self.itemSize = CGSizeMake(1, 1);

Disable UITableView horizontal scroll

I created a UITableView with a small frame, like (0,0,50,50). I want to disable the horizontal scrolling but keep the vertical scrolling.
I set self.table.bounces = NO, but the tableview can't be vertically scrolled, either. As a result, the animation is not so perfect.
So anyone has tips?
thanks!
Check if the content inset property of the table view is nonzero. If so reset it to zero.
change the content size of the tableView, make sure the width of the content size is not greater than the frame size
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, self.tableView.contentSize.height);
You can set AlwaysBounceHorizontal property of UITableView to false as follows:
Swift:
self.tableView.alwaysBounceHorizontal = false
Objective-C:
[self.tableView setAlwaysBounceHorizontal:NO];
self.tblViewProfile.alwaysBounceHorizontal = false // using coding
Or by storyboard/xib
Un tick horizontal bounce option in storyboard or xib
If you've applied tableView.contentInsetAdjustmentBehavior = , try selecting the value .scrollableAxes instead of .automatic or .always
If you created the table view in the interface builder then there is an option check the scroll vertically and uncheck the horizontal scroll.
If you didn't then you should try create it in the interface builder and assign it to your IBOutlet
Hope it helps !

Resources