Hide label/button when it's beyond the parent view - ios

I'm currently testing a UI element on tableview. There is view top of the tableview. So I've added a child view on that main view and added a label on the child view.When I'm scrolling the tableview,it calculate the content offset and change the parent view height. So I need to hide the label when it's beyond the boundaries of parent view, But it's keep showing the table. How can I make invisible label when it's beyond the parent view boundaries ?

view.clipsToBounds = true
or you can tick that on storyboard
this line forbid any subview from your tableview to go outside of the tableview frame.

Try this:
tableView.clipsToBounds = true
or
cell.contentView.clipsToBounds = true

Related

systemLayoutSizeFitting not returning correct height for view which has a table view

I need to dynamically set a container height. This is in the event of using a container view with view controllers which may change.
let frame = viewController.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
containerHeight.constant = frame.height
view.layoutIfNeeded()
I am using the above code for getting the required height of the new view controller so that I can set the container views height.
This seems to work fine on any view which doesn't contain a scroll view. If the view has a scroll view so for example I have a view with a title, tableview and button at the bottom of the tableview. The height calculated doesn't account for the tableview.
Is there anything special I need to do when calculating the height of a view which has a scroll view

How to change the height of the collection view in swift

I have a collection view and a text field on a ui view.
Initially, I have a text field at the top of the ui view and then I set the collection view top Anchor constraint to the bottom of the text field.
I want to implement a behavior where if I scroll down the collection view, the text field should disappear and the collection view's top should be at the top of the same ui view (this would hide the text field). When I scroll up, I want the constraints to be like the initial ones (container view's top Anchor should be set to the bottom of the text field). Any hint at how I might implement this behavior? I was hoping to implement this by updating the constraints when scrolling up and scrolling down happens. How can I implement this?
UICollectionView subclasses UIScrollView, so you can implement a UIScrollViewDelegate to get the scroll events.
Here is some code I use in one of my apps
The bottom of the text field is constrained to the top of the collection view
The top of the text field is constrained to the view's top layout guide - This is the constraint that is modified by the scroll view delegate code
As the collection view scrolls up the text field will move up and fade out
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let textHeight = self.textField.frame.size.height + 2
let offset = min(scrollView.contentOffset.y, textHeight)
if offset >= 0 || self.textFieldConstraint.constant != 0 {
self.textFieldConstraint.constant = -offset
let percent = max(1 - offset/textHeight,0)
self.textField.alpha = percent
}
}
}
Essentially you "push" the text field off the top of the screen, no more than its height+2
I think this solution can work full for you try this, just add a UIScrollView view as parent and put your text field and collection view in scroll view, as set the constraint as you set.

Collection View overlaps above when scrolling vertically

So I have collectionView but above that a UIView as well. When I scroll my collectionView vertically and go up, it scrolls and overlaps the UIView.
What could be the solution to make the UIView come on top of collectionView?
What did work was turning off clips to bounds in the collection View from storyboard.
Set the property "Clip to Bounds" to true. It can be done programatically or in the auto layout.
Programatically: colelctionView.clipToBounds = true
Auto Layout:
Your constraints should be like,
View - top,leading,trailing and fixed height
collection view - top,leading,trailing,bottom
and after that setup, you got issue then once try to set below in viewDidload,
self.automaticallyAdjustsScrollViewInsets = false
Try keeping View Below the Collection View in the View Hierarchy.
This looks sneaky, what let us know the effect.
Just play with the Section Inset

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.

How to add a search bar at the bottom of a table view in Xcode?

I am developing an iOS application, and I want to add a search button that initiates a search of a table view. When searching, the search bar should not be scrolled with the table cells.
To make a search bar (or any view really) "stick" to the top or bottom of a UITableView there are two approaches:
Adjust the frame of the table to be the view height minus the height of your bar, and then set the bar's frame to {0, 0, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(self.tableView.frame)}, which would position it statically at the bottom of the view. If you are using a stock UITableViewController, you'll need to do some additional work because self.view and self.tableView both point to the same object. You'll need to set a new UIView to self.view which will then be the container for the table view and your search bar view.
Add the bar as a subview of table, then implement UIScrollViewDelegate in your controller and use scrollViewDidScroll: (which fires whenever the user scrolls the view) to update the bar's position. The new position would be something like:
CGRect adjustedFrame = self.searchBarView.frame;
adjustedFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.frame) - CGRectGetHeight(self.searchBarView.frame);
self.searchBarView.frame = adjustedFrame;

Resources