I'm trying to display my UITableView's content under the status bar upon app launch (it can be achieved now by scrolling).
This is my current result:
But I'd like it to appear like this:
I've set these attributes for the UINavigationController
And I've tried to adjust the insets like so in viewDidLoad:
[self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
However, it doesn't display as desired.
Add the code in this method- -viewDidLayoutSubview()
[self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
set the tableView contentInsetAdjustmentBehavior property
tableView.contentInsetAdjustmentBehavior = .never
This is what worked for me.
tableView.contentInset = UIEdgeInsets(top: -UIApplication.shared.statusBarFrame.size.height, left: 0, bottom: 0, right: 0)
Related
How can I load a ViewController with the contentInset included on load?
[self.tableView setContentInset:UIEdgeInsetsMake(300, 0, 0, 0)];
Right now this includes an inset, but it's above the edge of the screen and I need to scroll up to see it.
I have a refresh control added to a collection view. Everything working, but when I set the collectionView.contentInset = UIEdgeInsetsMake(0, 20, 20, 20) the width of the refresh control stays the same but is shifted 20px, so is not centred in the view. The control should be reduced by 40px for it to be correct.
self.collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flowLayout];
self.collectionView.contentInset = UIEdgeInsetsMake(0, 20, 20, 20)
UIRefreshControl* pullDownRefresh = [[UIRefreshControl alloc] init];
[pullDownRefresh addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:pullDownRefresh];
I have tried setting the width manually after adding the view (also in viewWillLayoutSubviews), and playing with autoresizingMask to no avail.
Any ideas? I guess I will need to resort to putting it in a container view...but shouldn't need to!
OK, fixed the problem by not setting collectionView.contentInset. Instead I set on the flowLayout
i.e.
flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 20, 20);
The accepted answer didn't fit my needs, because I had a complicated collectionView with multiple types of cells and each had specific insets for sections, so I didn't want to dive in and add complexity with this solution.
What worked for me was pretty easy. Add the UIRefreshControl to the collectionView's backgroundView instead:
collectionView.backgroundView = viewModel.refreshControl
And when finishing the refresh, just do this:
DispatchQueue.main.async {
self.viewModel.refreshControl?.endRefreshing()
}
I need to have a button that looks like this:
Right now I went for a view with two buttons inside it. But the problem with that is that I can either hook up the two individual buttons to the same outlet, or add a UITapGestureRecognizer to the container view.
I went with the second option because I thought it would be cleaner, but that only works when I disable the buttons (and therefore disable the press-down animation).
Am I on the right track, and should I just find a way to trigger the button's animation when the UITapGestureRecognizer calls my method? Or is there a better way to do this?
You want to use a single UIButton and adjust the imageEdgeInsets & titleEdgeInsets.
For a Swift example:
extension UIButton {
func centerTextAndImage(spacing: CGFloat) {
let insetAmount = spacing / 2
imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
}
}
The size inspector tab for UIButton has an Insets section at the top. Play around with the insets values of title insets and image insets to achieve the desired result.
And you don't need to use multiple buttons in a view or tap gesture to achieve this. Just 1 single button would suffice.
I want to set offset offset for the first element of collectionView, so the first cell will apear in the center of the collectionView.
Put this in your ViewDidLoad:
yourCollectionView.contentInset = UIEdgeInsets(top: 0, left: yourCollectionView.frame.width/2, bottom: 0, right: 0)
I hope this helps
I did it with UIEdgeInsets. It can be set either in interface builder or in code by setting sectionInsets property of UICollectionViewFlowLayout
Sorry the question sounds a bit confusing. I have two buttons overlapping my table view at the bottom, so if the table view scrolls "normal" the last row is partially hidden by these buttons. That's why I want to allow scrolling the table like the height of one row further down, so the last row is on top of these two buttons. How can I achieve this?
Adjust the content insets of the table view.
For instance, if your buttons are 50 points in height and your table's frame is the full window, you could set your table to snap to the top of your buttons like this:
tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
Note: In iOS 7+ view controllers have a property automaticallyAdjustsScrollViewInsets that is set to YES by default. When this property is set to YES, the contentInsets you set manually may be overridden. Assuming you have a nav bar of some kind that you want to scroll under, you can set your top edge inset to the length of the topLayoutGuide.
Your final solution (put this in viewDidLoad):
self.automaticallyAdjustsScrollViewInsets = NO;
tableView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 50, 0);
As pointed out by others, deprecations have made this solution impossible, and if we use a section footer, this will display at the inset all the time. A much simpler solution would be to add a tableFooterView to allow the bottom cells to scroll past the buttons. Like so:
let bottomView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 83))
bottomView.backgroundColor = .clear
tableView.tableFooterView = bottomView
This will make it so the contents shift up.
If you want them to shift down you can change the insets accordingly.
func shiftScrollingUp() {
yourScrollView.contentInsetAdjustmentBehavior = .never
yourScrollView.contentInset = UIEdgeInsetsMake(0, 0, 150, 0)
}
AutomaticallyAdjustsScrollViewInsets is deprecated in ios 11.