What is the magical incantation for adjusting a UITableView's height when the keyboard is shown on-screen FOR ALL DEVICES? In the keyboardDidShow notification, I'm doing
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height - view.safeAreaInsets.bottom, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset
where the bottom constraint of my table view is Align Bottom to: Safe Area.
This works perfectly on all iPhone 5|6|7|8-type devices, but NOT on iPhone X* devices; the adjustment is not of a sufficient amount so as to move the bottom of the table up to meet the top of the keyboard. It's like ~58 pixels short.
Surely there must be some way to get this to work universally, eh? What am I missing?
This happens because iphones except iPhone X have no bottom safe area .
Try removing the view.safeAreaInsets.bottom.
Related
I am experiencing issues with setting contentInset on a tableView behaving inconsistently, depending on when it is called. I am using the following:
let edgeInsets = UIEdgeInsets(top: 52, left: 0, bottom: 0, right: 0)
tableView.contentInset = edgeInsets
tableView.scrollIndicatorInsets = edgeInsets
The top value is hard set to 52 there for testing, but in practice will be calculated from another view.
I am doing this because I need to have a view pinned to the top of a table view controller.
The behaviour I am experiencing is that if I use that code in the viewDidLoad or viewWillAppear functions, it works as I would expect. The tableView is below the pinned view and scrolled to its top (so all its content is visible). However, at that point, the pinned view has not been laid out, and its height is 0, so I can't use it to correctly set the top of the content inset (hence the hard 52).
If I use the above code from the viewDidLayoutSubviews function, which is where I have had it the whole time, it does not have the same results. The insets are actually set, but the tableView is also scrolled that much down, hiding its top rows behind the pinned view. I can then scroll up to see the top cells, and the tableView is then inset under the header, but it shouldn't start scrolled like that, and I have no idea why it is.
As you can see from the video, when the code is built and ran, the Red View is 0px from the top edge. But after scrolling down and tapping the status bar to return to the top, there is a gap between the top of the screen and Red View (i.e. the black area).
GIF of Simulator
What can I do to resolve the issue if the intended behaviour is for the Red View to be 0px from the top of display when the user scroll up to the very top?
Try setting a minus value for the scrollviews content inset:
scrollView.contentInset = UIEdgeInsets(top: -40, left: 0, bottom: 0, right: 0)
Your could also check the safe area.
This is a normal behavior in iOS. iOS will try to leave room for the status bar if you are using safe areas.
I have a UITableView where the last cell is cut off behind the UITabBarController. I set the bottom constraint of the tableView to the top of my bottomLayoutGuide. I also tried this approach without success.
The only solution that worked was adding insets to the content (one point was enough):
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 1, right: 0)
Therefore I wonder if that's a normal behaviour or a scroll view bug.
How to change spacing between the navigation bar and the first collectionViewCell?
I want to be aligned in the middle. In this case, the only way to calculate the screen size?
If you want to add space at the top of your collection view you can use contentInset. There are probably other ways to meet your requirement but this is the easiest way I can think of.
collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
I don't understand what you are trying to do. Can you explain more?
I have encountered something a bit strange with contentInsets
I have a UITextView in my storyboard with a contentInset of 50 left, as I'm trying to add some padding to my uitextview
However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar, so how can I remove the need for the horizontal scrollbar and make everything--the inset AND all the text in the uitextview--visible without the need for this scrollbar.
N.B: I'm not asking about preventing the scrolling horizontally or not displaying the scrollbar(thus cutting of the text)
Thanks a lot!
For atomk(UITextView is called ss)
NSLog(#"Content Size Before %f",self.ss.contentSize.width); Logs: 280
CGSize size=self.ss.contentSize; size.width=size.width-50;
[self.ss setContentSize:size];
NSLog(#"Content Size After %f",self.ss.contentSize.width); Logs: 230
There is no visible difference between the view with the code added than before it was added, so something's going wrong!
(Thanks)
In iOS 7 UITextView is based on TextKit and has a new property textContainerInset. It behaves just as you would expect:
UITextView *textView = ...;
// Left inset of 50 points
textView.textContainerInset = UIEdgeInsetsMake(0.0, 50.0, 0.0, 0.0);
Swift 4.2
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
UPDATE: This solution is out of date as of iOS 7.
See this answer below. In iOS 7.0, the textContainerInset property on UITextView was introduced.
Objective-C:
textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);
Swift:
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
Or as Zeev Vax suggested, in Swift 5.0:
textView.textContainerInset.left = 50
Pre-iOS 7 solution:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...
I'm afraid this is not how contentInset works with a UITextView. See Apple's documentation for contentInset where it states:
The distance that the content view is inset from the enclosing scroll view... Use this property to add to the scrolling area around the content.
The contentInset is added around the content.
You can change the contentSize in viewDidLayoutSubviews using the code you have included above:
- (void)viewDidLayoutSubviews
{
self.textView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0);
NSLog(#"Content Size Before %f",self.textView.contentSize.width); //Logs: 280
CGSize size=self.textView.contentSize;
size.width=size.width-50;
[self.textView setContentSize:size];
NSLog(#"Content Size After %f",self.textView.contentSize.width); //Logs: 230
}
However, this causes the text to be cut off on the right side:
The best way I have been able to achieve the appearance of horizontal padding in a UITextView is to position it inside a container UIView. In your case, simply create a UIView the same size as your current text view, and add a text view that is 50px narrower inside the container view.
This workaround can cause problems if you have a background for your text view, but from your screenshot above it doesn't look like that's an issue for you.
UITextView (frame in red) inside UIView container:
If your UITextView does have a background, see:
How to set UITextView's content inset like Notes App
Stuff you learn from reverse-engineering Notes.app (see "iPadding" section)