UIView detaches from bottom of UICollectionView on scroll - ios

I have a UICollectionView that allows a user to enter reps via a UIView containing a picker. The picker container attaches to the bottom of the view as long as the UICollectionView doesn't need to scroll. As more cells are added and the view must scroll, my picker container no longer attaches to the bottom of my view. I have attached a screenshot of my issue and code showing how I have attached my pickerContainer to the bottom of my collectionView.
let pickerContainer = UIView(frame: CGRect(x: 0, y: self.collectionView.frame.height - pickerContainerHeight, width: self.collectionView.frame.width, height: pickerContainerHeight))
<....some other code that i dont think matters....>
collectionView.addSubview(pickerContainer)

Don’t add it to the collection view as it will be added to scrollable area.
You can add it to the superview of collection view and put it above collection if you using storyboard. Or move in front using pickerContainer.superview?.bringSubviewToFront(view: pickerContainer) in case you interested in only in programmatically version.

Related

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.

How to insert custom view on the bottom of tableview?

In the case of loss network, I'd like to insert a custom view on the bottom of table view but not overlap the table view. This custom view still appear even the user leave their finger on screen and only disappear after a period of time or network come backs. It likes the behaviour of Facebook app. How can I do that?
//Give size of view as u want
let customView = UIView(frame: CGRectMake(0, 0, 200, 50))
//add this view in footer section of tableview
youTableview.tableFooterView = customView
You can do set footer as per below screenshot.

how to drag UIScrollView to the very right depending on its childs

This is a very simple question,
I have a UIScrollView where I am setting childView horizontally from left to right. each time I am adding a new view, I want my ScrollView to scroll to the right so that the newly added view is seen on the screen.
When you add a new view you need to first increase the scroll view's content size.
scrollView.contentSize = CGSize(width: childV1.frame.width + childV2.frame.width, height: scrollView.contentSize.height)
Then you should be able to call scrollRectToVisible and pass in the most recently added child view's frame.

Add a ScrollView to existing View

I'm developing a little app in Swift 2.0. I have a View with the following hierarchy:
Now, the elements placed in this view can't be displayed entirely in it, so I would like to use a ScrollView in order to be able to scroll all the content.
How I can embed all the content of my Viewto a new ScrollView? Can I do this programmatically by code?
UPDATE: There's an easier way to do this which I didn't know when I posted this answer
1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A. This will select all the elements in the view.
2) Go to Editor -> Embed In -> Scroll View. This will embed all the labels, views, etc into a scrollView.
3) Select the scrollview that you just embedded. Press Control and drag it to the respective class file of the viewcontroller and create an outlet scrollView.
4) Add this to your viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
Make sure the height for the contentSize is more than the size of your view.
ADDING SCROLLVIEW MANUALLY
1) Drag and drop a scrollview from the Object Library onto your viewcontroller in the storyboard and create an outlet to your program as 'scrollView'.
2) Click on your viewcontroller and go to the size inspector and note down the width and height.
3) Click on your scrollview and set the width and height the same as the viewcontroller. and set the X and Y values to 0
4) Click on the scrollView and drag it a little bit to the side
5) Press Command+A to select all the elements including scrollView. Press Command and click on the scrollView to deselect the ScrollView
6)You will have all the elements except the scrollView selected now. Now click and drag them into the scrollView.
7) Now click on the scrollView and set the X and Y values to 0 from the Size Inspector.
8) Add this to your viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
Make sure the height for the contentSize is more than the size of your view.
That should create a scrollView for your view. Some of the elements might be in a slightly different position. You can easily fix them by moving them on your storyBoard.
It can be done even simpeler than ebby94's answer.
1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A. This will select all the elements in the view.
2) Go to Editor -> Embed In -> Scroll View. This will embed all the labels, views, etc into a scrollView.
3) Set the constraints of the Scroll View to the View's edges.
And you're good to go! No need for an outlet.
If you are using Snapkit or creating programmatically.
class ScrollViewController: UIViewController {
lazy var contentViewSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 320) //Step One
lazy var scrollView : UIScrollView = {
let view = UIScrollView(frame : .zero)
view.frame = self.view.bounds
view.contentInsetAdjustmentBehavior = .never
view.contentSize = contentViewSize
view.backgroundColor = .white
return view
}()
lazy var containerView : UIView = {
let view = UIView()
view.frame.size = contentViewSize
view.backgroundColor = .white
return view
}()
override func viewDidLoad() {
self.view.addSubview(scrollView)
self.scrollView.addSubview(containerView)
//Now Set Add your Constraints in the container View.
}
}
Above accepted answer explanation is enough to achieve the scroll view but I would prefer to create my complete application programatically and I don't use storyboards in my project. This code is for the folks who don't prefer to use storyboards.
Explanation
Step One: Determine your content Size. Here I am taking Exact width and adding 320 more to the height of the screen.
Step Two: Create a scroll view and add desire behaviour of the scroll view. Now, the contentSize of the scroll view should be same as the contentSize you've created above at step one.
By Following Step one and Step Two. You Will be able to set a scroll view on the top of the view. But If you want to add a stretching behaviour then You should follow Step Three
Step Three: Create a container view of the same size of the contentView which you've calculated in step one and set it to the frame of the containerView. By doing this you'll be able to achieve stretching header and footer behaviour in your screen.
Please read make sure to add constraints in the same order as it is set.
Answer Edits are welcome.
I had the same issue. I needed to add scrollview to the existing view.But my main container view has a lots of view inside it. And they were connected to each other. So i was afraid. Finally i did it. the process given below.
Duplicate your View (root View). For this first select the root view then press Command + D
Now delete all the child view view inside the root view
Now add a scroll view to the root view and set constraint to 0,0,0,0
Now add the duplicate(That you duplicated) view to the scroll view and set constraints to 0,0,0,,0 also set the height that you want.
Set width of the duplicate view by equal width with the root view.
Now select the viewController, go the size inspector, select free form size. Then set the height that you entered with duplicate view.
You have almost done. Now you have to connect the child view with outlet or action that you gave in the viewController class.
Thats all.
Selecting all elements and embedding scroll view (editor->embed in->scrollView) works fine.
Adding constraint is much more easy by selecting the constraint warning (Add Missing Constraints).
It's simple. Command A and Command X to specific view controller in StoryBoard. After that take scroll view. On scroll view, just take one view with view controller view height and width equal to scroll View width. Again do Command V and rearrange the constraints. Your problem will be solved.

Sticking a TableView Header to the top, causes the header to not "interact" when user scrolls down

I am sticking my UITableView header to the top when user scrolls down the UITableView. The header view itself is a UIButton which does something when clicked.
The button responds well to touches when contentOffset Y is 0. However when the user scrolls down, the button still sticks to the top but every touches "passes through" it.
Here is my code to stick the header to the top:
var offsetY = scrollView.contentOffset.y;
var headerContentView: UIView = self.tableView.tableHeaderView?.subviews[0] as UIView;
headerContentView.frame = CGRect(x: 0, y: max(0, offsetY), width: headerContentView.bounds.width, height: headerContentView.bounds.height);
Thanks.
If you're going to be moving the view around yourself, don't use tableHeaderView at all. Instead add it as a subview of the table view directly and keep a reference to it. Then in scrollViewDidScroll: layout the view's Y offset according to scrollView.contentOffset.y.
You may need to trigger this layout in viewDidLoad so that it appears properly before any scroll events happen. If the view shouldn't overlap the top cell when the table is scrolled to the top, set the view's height to the table's contentInset's top.

Resources