how to make uitableviewcontroller at the bottom of the screen - ios

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)

Related

UICollectionView shadow doesnt show up correctly

so I'm making an tabbarcontroller and in my tabbarcontroller have 2 collectionview, collectionview 1 height is about 50px and the 2nd is right below it with constraint 0 to all side and I'm trying to set the shadow for the colletionview1 without the collectionview2 the shadow show up correctly but with the collectionview2 or any UIView right below the collectionview1 they seem to cover up the shadow that I set for collectionview1
func setupMenuBarShadow() {
menuBarCollectionView.layer.masksToBounds = false
menuBarCollectionView.layer.shadowColor = Theme.darkShade?.cgColor
menuBarCollectionView.layer.shadowOpacity = 0.6
menuBarCollectionView.layer.shadowOffset = CGSize(width: 0, height: 0)
menuBarCollectionView.layer.shadowRadius = 4
}
That is my code for setting up the shadow for the colletionview1
as you said without 2nd collection view the shadow show up correctly then I think the problem is the constraint you provide to the 2nd collection view which 0 form all side. you can try 2 things
1: change the background color of 2nd collection view to clear
2: have some space between the 1st collection view bottom and 2nd collection view top.

Make cell stick to the bottom of a tableview

Hello I am trying to figure out how can you stick the last cell/section on a tableview to the bottom screen. This is the screenshot.
I am trying to put the create account at the bottom. I've tried using the tableview footer but this is what I get.
How would I be able to put that red view at the very bottom. I know I can just use a view controller and drag a table view controller and place the button at the bottom. However I would like to learn how to do this using the tableview controller. Would really appreciate any help :)
This is the table view footer code I am using in the viewDidLoad. Which creates the red view at the bottom of the create account.
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
footerView.backgroundColor = .red
tableView.tableFooterView = footerView
Okay, I see what the issue is.
When you assign the UITableView.tableFooterView it puts it at the end of all the cells. It's not fixed to the bottom of the table.
Because UITableViewController does not have a parent view outside of the tableview, there is no way to add a view to the tableview that will not also cover up additional cells.
For example, you could programmatically create a UIView, add constraints, and then add it as a subview of the tableview - this would likely have undefined behavior if the table view ever changes the number of rows in it.
Even if it worked, it would likely cover up cells as you scrolled.
Unless there is some external constraint, I recommend you use the correct tool for the job - make a UIViewController and put a UITableView and your red view underneath it.
Add your button on storyboard under of your tableview. in your case there is no reason to have it in a tableview cell
You may use a little trick: as you know, header stick to the top of the table view. What you can do is mirror the table view and its contents horizontally by applying scale transform, like so
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
also, in your cellForRowAt
cell.transform = CGAffineTransform(scaleX: 1, y: -1)
and in viewForHeaderInSection
headerView.transform = CGAffineTransform(scaleX: 1, y: -1)
Just make your Create Button a section header and apply transforms.
Alternatively, you can simply add that button as a subview to your table view on the bottom, and provide an empty view as section footer view, with height equal to your button's height.

Button over tableviewcontroller stretching (Swift)?

I have a button in a view which is in the footer of a tableview (UITableViewController). Why is the button stretching when I try to apply the following code to it?
And I apply the code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Make footerview so it fill up size of the screen
// The button is aligned to bottom of the footerview
// using autolayout constraints
self.tableView.tableFooterView = nil
self.footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.tableView.frame.size.height - self.tableView.contentSize.height - self.footerView.frame.size.height)
self.tableView.tableFooterView = self.footerView
}
I was following the answer on this question:
Add button on top of UITableViewController (Swift)
Thanks!
If you read the question (whose link you posted) carefully, you can see that he resizes the view to take up the rest of the screen that is remaining after your table. Your button is the same size as your view, that is why it is stretching up. You need to add constraints which bind your button to the bottom of the view but not the top. Because if you bind the top and the bottom both to the view it will stretch.
Here is the example.
1. When you do not bind the button to the top. Notice that in the constraints, I do not have any constraint that specify the top of the button.
When you bind your button to the the top of the view. In this, I set a constraint which bind the button top to the view top. It stretches my button to take up the whole space as the view(which is similar to your case)
Hope this helps. :)

Howto fix TableView right margin? (swift)

I've created a tableview within Viewcontroller and inserted disclosure indicator, but it is not showing, because the right margin is not correct.
What I tried:
selecting table and click in xCode on "pin" button and set left and right to 0, select there all frames in container and clicked: add constraints
but without results.
How can i fix this?
I attached the problem as image. (i have added a frame to focus the problem)
Okay, there can be multiple reasons for which this can happen. For instance, one can forget to deck the cell with wrong identifier or may have set the accessory Indicator in a wrong way. My personal assumption is you probably have set constrain improperly.
So, I am just going to show you the whole process.
Step 1: Drag and UITableView inside your ViewController and drag a UITableViewCell inside that TableView. Select the Table View Cell and assign an identifier. Make sure, in your datasource method, -cellForRowAtIndexPath, you use this same identifier.
Step 2: Go to your View Controller and take an IBOutlet of a UITableView.
#interface MyViewController : UIViewController<UITableViewDataSource>
#property(nonatomic, strong) IBOutlet UITableView *myTableView;
#end
Step 3: Go to the connection Inspector of your storyboard and connect the UITableView with this outlet.
Step 4: Go to the implementation file of your View Controller and populate the datasource methods. Also make sure, you set your TableView's datasource to your view controller.
Now when you build and run this code you won't see the accessory Indicator.
The reason is you didn't set the constraints.
Step 4: Just set the constrains like the following and you are good to go.
This is my final view.
You need to change seperator inset inorder to remove left margin. Its set to default change it to custom and make left insets and right insets to zero. You can set the 'Separator Inset' from the storyboard:
Change Seperator insets to Custom and make left to zero
Try to programmatically set the width and height of the tableView like so...
#IBOutlet weak var resultsTable: UITableView! // connect to tableView
override func viewDidLoad() {
super.viewDidLoad()
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
resultsTable.frame = CGRectMake(0,0, theWidth, theHeight)
}
The problem was not the margin/layout.
To show disclosure indicator in tableview cell's, it is needed to select disclosure indicator from Accessory menu AND give a identifier for the cell.
Without given identifier the disclosure indicator is not showing.

custom tableView's header view overlap the table view cells

I designed a custom view as my UITableView's header view. just like this
(I just put image link here instead of image since I don't have 10 reputations.)
http://i.stack.imgur.com/KhNbE.png
Then in my UITableViewController I use this view as tableHeaderView
override func viewDidLoad() {
tableView.tableHeaderView = headerView!
//...other things
}
I got text from a JSON to fulfill the ContentLabel. If the text is long, the headerView will overlap cells just like below image.(short text is OK)
http://i.stack.imgur.com/gtO2g.png
Section is visible but two lines of cell have been overlapped by the headerView.I'm not sure if I did wrong constraints or code on ContentLabel. Below is the code I configured the contentLabel in TopicHeaderView.swift
var content: String? {
didSet {
self.contentLabel.text = content!
self.setNeedsUpdateConstraints()
self.updateConstraintsIfNeeded()
self.setNeedsLayout()
self.layoutIfNeeded()
}
}
func setFrameHeight(height: CGFloat) {
var frame = self.frame
frame.size.height = height
self.frame = frame
}
override func layoutSubviews() {
super.layoutSubviews()
self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.alignmentRectForFrame(contentLabel.frame).width
self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width
self.authorLabel.preferredMaxLayoutWidth = self.authorLabel.bounds.size.width
self.setFrameHeight(CGRectGetMaxY(contentLabel.frame) + 8)
}
I browsed similar questions in SO but seems I can't find a solution to fix my problem. Can anyone help on this?
EDITED:
I logged the origin CGPoint of my first tableView cell and headerView's height. It shows the right number which means the first cell is right next to the header view vertically. There is a 22 points gap because of the height of section of course.
headerheight:600.0
first cell's y: 622.0
So maybe it's the label problem that its height is too big to exceed the bounds of TableView headerView? I'm not sure.
EDITED:
Strange things happen. I logged the y value of headerView's bottom,contentLabel's bottom and first UITableViewCell's origin. Please see the image from the link in the question comment below(still need 10 reputation)
As you can see, from the value in console, the view sequence from top should be "contentLabel's bottom(value:224) - headerView's bottom bounds(value: 232) - first cell's origin(value:254)". But in simulator, the sequence is totally messed up.It turns "headerView's bottom bounds - first cell's origin - contentLabel's bottom"
I really appreciate if anyone can help on this.
Problem is, that UITableView does not automatically change positions of cells when its headerView's height changes. Thus you need to reload UITableView every time TopicHeaderView.content changes.
Select that header view, or imageView what you have there, and check Clip Subviews in Attributes Inspector tab.
This worked for me.

Resources