Swift Show Specific View Controller After Facebook Login Updated FREE
Swift Show Specific View Controller After Facebook Login
How To Present a Bottom Sheet View Controller in iOS
A unproblematic and easy approach with no library needed
A bottom sheet modal view controller is a feature that has a great UI/UX pattern. It is very useful when nosotros want to present a quick preview with minimal content. In fact, in most iOS implementations, it has a pan gesture feature (draggable) to simply dismiss the controller or expand the content by dragging the view to the top or bottom. The modal view controller can also exist speedily dismissed by tapping the dimmed background.
This feature is currently implemented in many well-known apps such as Facebook, Slack, and Instagram.
What Will We Build?
In this article, we are going to create a elementary app (shown in a higher place) with a modal view controller that has the following capabilities:
- Can be presented modally with the content size taking up around half of the screen size or less (customizable).
- Able to drag down to dismiss the view controller (using pan gesture recognizer).
- Able to drag upward to expand the view controller.
- Able to dismiss the view controller by tapping on the dimmed background.
I assume everyone reading this is already familiar with Motorcar Layout and modifying constraints. In addition, nosotros will build this programmatically, including creating views and setting up the layout constraints.
However, y'all may apply Storyboard, Xib, or other Machine Layout libraries. Just make sure to focus on which constraints will have the changes and impact the layout throughout the tutorial.
1. Create New UIKit Projection
Create a new project with UIKit and Storyboard. Withal, we are not going to update the Chief.storyboard
file, every bit we will create the whole thing programmatically in a Swift file.
Remove Storyboard (optional)
You may completely remove the Storyboard with the following actress steps:
1. Kickoff, remove "Main" from Project > General > Master Interface
.
2. Open up Info.plist
file as a "Source Code" and remove these two lines:
<key>UISceneStoryboardFile</key>
<string>Main</cord>
3. So, delete the Primary.storyboard
file from the projection.
iv. Add together the code below to theSceneDelegate.swift
to set up our root Window from the specified View Controller.
2. Adding Views to ViewController
In this project, we will be working on two principal view controllers:
-
ViewController
already exists inViewController.swift
every bit a main page. -
CustomModalViewController
is a presentable view controller that we will create in the next step.
Now, open ViewController.swift
and we are going to add some text and a push that are wrapped in UIStackView
. This button will trigger a function to present our custom modal view controller.
Update your ViewController.swift
file with the lawmaking below and run the projection (command + R). Yous should exist able to run across the same results as shown in image in a higher place.
Below is an explanation of each important indicate added in the view controller:
- Add all the needed UI components, including
UILabel
,UITextView
, andUIButton
, inside aUIStackView
with the lazy closure and define the styles. We are not setting the frame or size because it will be handled in the constraints setup in the next steps. I also added aspacer
inside thecontainerStackView
to push theregisterButton
to the lesser of the screen. - Our common
viewDidLoad()
to phone call all the setup methods. - Bind an action method to the
registerButton
. - The
setupConstraints()
method is where we add together necessary subviews and ready the constraints. I think the constraints are quite straightforward, although they are written programmatically. - Wrap all the constraints needed in the
activate()
function to immediately update the layout of each view. - Set the
containerStackView
edges (pinnacle, bottom, leading, and trailing) to superview with 24px spacing. - Prepare the button height to 50px.
- Prepare a
presentModalController()
method that will exist called when we tap on theregisterButton
.
Smashing, at present nosotros are done with the first page. Let's go along to create our 2nd view controller.
3. Create CustomModalViewController
Create a new controller called CustomModalViewController
(y'all may use any suitable name). Then add a UIView
called containerView
every bit the primary view to concord subviews of UI components. The image below shows the 2nd view controller that we are going to build in this step:
In the initial stage, nosotros will create the CustomModalViewController
with an empty view and without animations. Beneath is the code that will update the view controller:
- Create a
containerView
as the main place to go along the content later. - Create a simple
dimmedView
as a back dimmer. - Create dynamic constraints that are subject field to be changed based on blitheness and
containerView
height. - Add
dimmedView
andcontainerView
equally main subviews. - Set the static (unchangeable) constraints.
- Gear up the container height to default (300px).
- Fix the bottom anchor of the
containerView
to0
and so the view volition be visible from the lesser border to 300px tiptop.
Then, update the presentModalController()
method in ViewController.swift
as shown beneath:
Nosotros are not using present()
with blitheness as shown in the code above. Thus, we should set animated
value to false
. Instead, we volition add our custom animations to animate the dimmed background and container movement.
Now, if y'all run the project and click the "Get Started" push button, it volition show our new view controller without any animation withal.
iv. Present Container View With Animation
Add a new method called animatePresentContainer()
that will present the containerView
from lesser to default tiptop. In setupConstraints()
, brand sure to alter the initial bottom constant to defaultHeight
(300px) so the containerView
volition initially hide below the bottom edge.
It is important to always keep in listen that if the lesser anchor is equal to 0
, the containerView
will visible. If the bottom ballast is greater than 0
, it will be slightly hidden based on the outset value.
This new method needs to be called from viewDidAppear()
.
5. Adding Dimmed View Animation
Add together a new method called animateShowDimmedView()
. Then make a simple fade animation past updating the alpha value, as shown in the code beneath. This method will also be called in viewDidAppear()
with animatePresentContainer()
.
6. Calculation Custom Dismiss Animation
In this pace, we are going to imitate the view controller dismiss animation. Commencement, add a new method called animateDismissView()
. It is used to hide the dimmed view and move the containerView
below the bottom edge. At the end of the animation (in the completion closure), it will call the native view controller'south dismiss()
method without whatever animation.
One time this is completed step, our app should be able to smoothly present the bottom containerView
along with a fade dimmer blitheness when the "Get Started" push is pressed. Also, y'all should be able to dismiss the view controller by borer on the dimmed view.
7. Gear up Upwardly Pan Gesture To Discover Dragging Motility
This part is important because we want to capture the distance of the showtime value while panning on the containerView
.
1. Add a new method chosen setupPanGesture()
to set up the gesture on the master view and demark it to the handler method.
ii. Create a handler method chosen handlePanGesture()
. For improve agreement, permit's practise a simple printout to capture the dragging altitude and direction:
three. At present, phone call the setupPanGesture()
in viewDidLoad()
.
Run the project and effort panning the containerView
. When checking the lesser output logs, it should print a like result to what is shown below:
Pan gesture y offset: -20.666671752929688
Dragging direction: going upward Pan gesture y start: -14.333343505859375
Dragging management: going upwardly Pan gesture y offset: -three.6666717529296875
Dragging direction: going up Pan gesture y starting time: 9.0
Dragging direction: going down Pan gesture y start: xiv.333328247070312
Dragging direction: going downwardly Pan gesture y offset: 22.666656494140625
Dragging direction: going down
8. Update Gesture Handler
From the consequence in the previous footstep, we know that if the drag management goes upwards, the Y offset is always minus — and vice versa. Therefore, in order to update the containerView
with the new height, we should implement this formula:
newHeight = containerHeight + (-translation.y)
Equally a result, when we drag to the lesser, the container height will get smaller. When we drag to the top, the height increases. Perfect!
Expand and dismiss weather
Remember that we have determined the value of our condition.
- Default Height: Stock-still default size of the container
- Maximum Height: The maximum value that the container can present or be dragged. Whenever the
containerView
is dragged to the peak from the default height (300px), whether the user releases or not, thecontainerView
will keep growing to the top (cannot exceed maximum height). - Minimum Height: If the container reaches the minimum value or goes lower than that, the view will be automatically dismissed. It is like to a "swipe down to dismiss" activity.
Now, add together some allow
constants for the fixed values and also a var
to keep the changes to the container'southward acme.
Then, update the handlePanGesture()
based on the gesture state and add together a new animateContainerHeight()
method to update the new container height with blitheness. The meridian of the container is updated based on the dynamic constraint variable called containerViewHeightConstraint
. Run into the comments in the lawmaking for a detailed explanation:
ix. Adding Content to Container View
This stride is optional if you desire to see the content changes when the containerView
is expanding or shrinking.
We are going to add two labels and a stack view, and so set the constraints of the stack view to the border of the superview (containerView
).
Project Completed
Congratulations! At present, we have completely washed all the steps and the project is gear up to run. We accept written a very minimal code and the custom modal view controller is working nicely.
The complete source code can be downloaded from my GitHub repository. Attempt to implement it into your projects and improve the code by creating a wrapper class that will be reusable in other view controllers.
Thanks for reading. Happy coding!
Swift Show Specific View Controller After Facebook Login
DOWNLOAD HERE
Source: https://betterprogramming.pub/how-to-present-a-bottom-sheet-view-controller-in-ios-a5a3e2047af9
Posted by: reevesruslaideemin.blogspot.com