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.
Related
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.
I have a view with an image and a tableview. The image is in the first half of the screen (portrait) and the tableview in the second half. And now I want to move the tableview over the image until it reaches the top and covers the image. Then it should start the "real scrolling".
But what's the best way to do this? Can I like replace the touchesMove of the variable tableView? I could create an extension of the UITableView and override the function, but then I don't have access to the view of my Controller to move the tableView.
Any answer? Thanks!
The imageView should be behind the tableView with constraints top, leading, trailing to superview and height to superview with a multiplier of 0.5. The tableView should fill its superview.
The trick is that you add a tableViewHeader that is invisible and equal to half the height of the screen. This has the effect of pushing the initial content of the tableView off the screen. In interface builder add a UIView to the tableView as header and make it transparent. Also make the background of your tableView transparent. Take an outlet to the headerView and the tableView. In viewDidLayoutSubviews set your headerView.frame.size.height = tableView.frame.size.height / 2.
Apart from what Josh answered, what you can try is:
What you can do is make the UIImageView's height decrease based on the scroll amount until the UIImageView's height is 0 and then start the scrolling otherwise force the contentOffSet of the UITableView to always remain 0. That being said, here is how to do it:
Make an enum to keep track of the various states of the UIImageView like:
enum Layout {
case ImageExpanding,
case ImageDefaultHeight,
case ImageDiminishing,
case ImageNotVisible
}
In your storyboard, add a top, leading and trailing constraint to the superview for your UIImageView and fixed height constraint of lets say 200(don't worry you will change this later). To your UITableView add a leading, trailing and bottom constraint to the superview and a top the UIImageView. Now drag and drop a constraint outlet for the UIImageView height into your UIViewController.
In your viewDidLoad set your heightConstraint to be 1/2 of the total screen height and set the enum state to initially be ImageDefaultHeight
Now in your scrollViewDidScroll you will have to check the direction of the scroll and based on that while checking the current state of the image, increase or decrease the heightConstraint based on the amount a user scrolls by and always check:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if(/*up*/){
//just telling you about this if condition as this will ensure that
//if your table view is large and if tableview isn't at the top,
//the image shouldn't expand
if layout == .ImageNotVisible{
if scrollView.contentOffset.y <= 0 {
//start expanding once you reach top of tableview
layout = .ImageExpanding;
}
}
//keep track of other enum states and update your uiimageview
//appropriately after calculating the user scroll amount
//until the height reaches your initialDefaultHeight and
//then do nothing. You will have to figure out the code
//for this on your own
}else if(/*down*/){
//make image smaller
}
//dont let table view scroll until image height is 0 when use is scrolling down
if layout != ImageNotVisible{
scrollView.contentOffset = CGPoint(x: 0, y: 0)
}
}
I am very new to iOS. Can anyone tell me how to fix this, to fill all my view with UITableView:
I am trying to do it with auto layout and constraints but I don't know why isn't working.
What you want to do is to have the cells at equal height so that all cells together fit exactly the height of the screen.
You cannot do that with auto layout. You have to tell the table view the height you'd like the rows to have like so:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let heightOfVisibleTableViewArea = view.bounds.height - topLayoutGuide.length - bottomLayoutGuide.length
let numberOfRows = tableView.numberOfRowsInSection(0)
tableView.rowHeight = heightOfVisibleTableViewArea / CGFloat(numberOfRows)
}
Note that this code assumes that you don't implement the method tableView(_:heightForRowAtIndexPath:).
To fill the whole view, with a table view, I use four constraints:
One. tableView centre = view centre.
Two. tableView width = view width.
Three. tableView vertical distance to top layout guide = 0
Four. tableView vertical distance to bottom layout guide = 0.
To fill everything with the same color, I set the backgroundColor of the tableView, then set the backgroundColor of the cells to [UIColor clearColor].
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)
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. :)