Spacing between navigationBar and tableview cell - ios

How can I add a spacing between navigationBar and first UITableViewCell?

You need to add contentInset to your UITableView the first value is for top inset the second is left third for bottom and the last for right
Like this
self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0) //replace 10 by your needed value

You don't need to do it from the cell, but from the tableview:
go to your storyboard where your tableview is, and change the top constraint of your tableview to be something more than the 0 that it currently is.

Related

Swift UITableView: How to remove space under the last cell

I have a normal UITableView with two cells. Every cell has a height of view.frame.size.height - 250 in the heighForRowAt method.
But under the last cell a white space appears. Look:
But I would like that it is like that:
Does anyone know how to do this?
Warm greetings
PS: The bottom constraint of my tableview is 0 = superview
Add tableview bottom constraint with superview rather than with Safe Area.
The issue is because of tableview bottom constraint with Safe Area.

iOS - standard insets for tableview cell separators

as the title states, I'm wondering what the standard insets are for the default iOS tableview is. I'm using a different library and want to set the insets equal to the default table view cells.
You can use insets property given in storyboard or you can give insets programmatically.
Like,
[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; // Top, Left, Right, Bottom

Dynamic height for UITableView ( Not Cell )

I'm trying to have a UITableView that lists all the different HomeKit devices a user has available.
Obviously there is no way to know how many devices they have, so I need to have the UITableView's height in the storyboard change.
I've tried this, which I call in the viewDidLoad() function:
func adjustHeightOfTableView() {
//getting the height of the tableview
var tableHeight = self.tableView.contentSize.height
//the height of the content inside the view
var maxHeight = self.tableView.superview?.frame.size.height
//if the height of the content is bigger then the height of the tableview
if (maxHeight! > tableHeight) {
tableHeight = maxHeight!
//set the tableview height to be the content height
}
//trying to reload the tableview height?
self.view.layoutIfNeeded()
self.tableView.reloadData()
}
I am trying to have some UI Elements under the tableview, and I want them to be a set space from the bottom of the tableview, but also have the tableview be the height that it needs to be, for whatever amount of cells there is.
But it's just not working.
If I'm doing anything wrong, or if anyone knows how to make this work, please let me know.
Thanks!
Note: For this approach you need to have static cell height or figure out a way to know before hand whats the total contentsize height
Assuming you are using constraints, create following constraints on your UITableView (apart from leading and trailing!)
Add a height constraint with a priority of 750 and a bottom spacing constraint of 0 to your super view that will be >= 0 and have a priority of 1000. Create outlet for this height constraint that you created in your UIViewController
Now,
func adjustHeightOfTableView() {
//set the height to be equal to the number of elements multiplied by the height of each cell.
//or use some logic that allows you to know what content size or space the cells will occupy!
tableViewHeightConstraint.constant = dataArray.count * rowHeight
view.layoutIfNeeded()
}
Now if your UITableView height is less than super view, no problems! But if it is greater than screen bounds, it will break the height constraint and become full screen and display the content normally as you expect a UITableView to!
Edit:
Even if you are using UIAutomaticRowDimensions what you can do is add constraints programmatically to your UITableView. i.e
Of course all your other views will still have a bottom constraint to your UITableView.
Create a UITableView in your storyboard with normal leading, trailing, top and bottom to the super view. Fetch the data. Get the contentSize for your UITableView and then remove the bottom constraint. Now add a height constraint that will be the minimum value of your UIScreen.main().bounds.size.height and contentSize.
you can use Automatic Dimensions if you are using autolayouts
in view didload:
let nib = UINib(nibName: "YOURCELLNIB", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "REUSEIDENTIFIER")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
Remove the function
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)
In your code, you have:
tableHeight = maxHeight!
//set the tableview height to be the content height
But this does not change the table height - it only changes some variable that previously was assigned the value of the old table content height. Nowhere in your code do you actually do anything to change the table height.
One way to change the table height directly is to assign it a completely new frame with values from the old frame, except for the frame's height, which you calculate however you like.
Try something like this (adding whatever other logic you need):
oldFrame = self.tableView.frame
newHeight = rowCount * rowHeight
self.tableView.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, newHeight)
There is a workaround which can make it seems like the height changes according to the number of the cells.
set tableview height to a proper value when init.
UITableView.init(frame: CGRect.init(x: 0, y: 70, width: self.view.frame.width, height: self.view.frame.height - 350))
set the tableview background color white transparent.
pulldownTableView?.backgroundColor = UIColor.white.withAlphaComponent(0)
set tableFooterView.
pulldownTableView?.tableFooterView = UIView(frame: CGRect.zero)
Below is the result, there are two table in the img. I set the transparent for the front tableview, left img set the backgroundColor to white, right white transparent.
----------------------vs----------------

how to make uitableviewcontroller at the bottom of the screen

i have a uitableviewcontroller with static cells, they are 4 cells.
so the result is like this:
as you see there is a white space at the button, i want to change the color of it, i change the color of the view that comes with the UITableViewController, but that didn't help. i read i understood that i have to make the table view scrol to the buttom of the screen,
i tried this code (i found on internet)
if tableView.contentSize.height > tableView.frame.size.height
{
let offset = CGPoint(x: 0, y: tableView.contentSize.height - tableView.frame.size.height)
tableView.setContentOffset(offset, animated: false)
}
but nothing changed, i want to ask you if you know a solution please
If you use storyboard
1.- Select your tableView.
2.- Go to the Inspector and change color of your background in "View"
3.- Run
Just change the background color of your tableView just add this tableView.backgroundColor = UIColor.blueColor() the tableView should be at the top of the view in my opinion, so I would recommend to change the background color.
Add this row in your viewDidLoad()
There are different ways to achieve it:
Set the height of cell equals to Screen Height / 4
add a constraints to UITableView to the the bottom of the container
of the UITableView and set its value to 0 (from storyboard)

TableView ContentInset does not shrink cell width / Add horizontal padding to TableView contents

I'd like to have a UITableView which is full screen. But the content of the UITableView should have a padding on the left and right.
So I tried to set ContentInset. But now the cells are as wide as the UITableView and the UITableView scrolls horizontally.
Is there a way to say that the UITableView content's width should become narrowed by the horizontal content insets? Or do I have to add the padding to all cells and header/footer views?
I don't want to narrow the table view itself, because the scroll indicator should stay at the right side of the screen and not in the middle.
The here (How to set the width of a cell in a UITableView in grouped style) suggested solution seems to be not as generic as i'd love to, beacuse the cells and header and footer views have to know about the padding (at least 3 places to maintain instead of one)
I don't want to narrow the table view itself, because the scroll
indicator should stay at the right side of the screen and not in the
middle.
This makes you happy?
_tableView.clipsToBounds = NO;
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -30.f);
If you don't like clipsToBounds = NO effects, you can embed the tableView in container view which is clipsToBounds = YES.
Set the layout margins of the table view. For this to work make sure your constraints in the cells are set relative to the superview margin.
tableView.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)

Resources