Use UICollectionView in Swift

karthikeya Boyini
Updated on 30-Jun-2020 05:58:40

520 Views

To use collection view in swift, first, we need to create a collection View. We can either drag and drop it to the storyboard, or we can make it programmatically. After that, we need to confirm our class to UICollectionViewDataSource and UICollectionViewDelegate. Also if we need custom cell size and layouts, we need to confirm it to UICollectionViewDelegateFlowLayout.Let’s see the step required to create a collection View programmatically.func initCollection() {    let layout = UICollectionViewFlowLayout()    layout.itemSize = CGSize(width: 50, height: 50)    let collection = UICollectionView.init(frame: self.view.frame, collectionViewLayout: layout)    collection.dataSource = self    collection.delegate = self    collection.backgroundColor ... Read More

Create Custom Dialog Box on iOS App Using Swift

karthikeya Boyini
Updated on 30-Jun-2020 05:57:47

897 Views

To create a dialog box in swift we’ll make use of UIAlertController which is an important part of UIKit. We’ll do this with help of an iOS application and a sample project.First of all, we’ll create an empty project, then inside its default view controller, we’ll perform the following operations.We’ll create an UIAlertController object.let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)We’ll create an actionlet okAction = UIAlertAction.init(title: "Ok", style: .default) { _ in    print("You tapped ok")    //custom action here. }We’ll add the action to the alert and present italert.addAction(okAction) self.present(alert, animated: true, completion: nil)Now we’ll convert this ... Read More

Handle Right-to-Left and Left-to-Right Swipe Gestures on iOS App

karthikeya Boyini
Updated on 30-Jun-2020 05:56:36

2K+ Views

To handle gestures in iOS application we’ll create an application with swift and see with help of an example. This can be done in two ways, with storyboard or programmatically.Method 1 − With storyboardFirst we’ll drag a swipe gesture recognizer from our object library and drop it in our View controller in which we want to add the swipe gesture.Then click on the gesture, press control and drag in your view controller class to create its connection.Make sure the sender of that action is UISwipeGestureRecognizer and the action looks something like this:@IBAction func swipeMade(_ sender: UISwipeGestureRecognizer) { }Now the swipe ... Read More

Display Image with Rounded Corners in iOS App using Swift

Samual Sam
Updated on 30-Jun-2020 05:55:51

3K+ Views

To make an image with round corners or to make any view or button or any UI element with round corners in swift, we need to access the corner radius property of its layer. Every UI element in iOS is based on a layer.First of all, let’s add an UIImageView Object in our storyboard. Or let’s create one programmatically.Below is a function that will programmatically create an image view and add an image to it.func addImage(imageName img: String) {    let imageView = UIImageView()    imageView.frame = CGRect(x: 10, y: 20, width: 200, height: 200)    imageView.contentMode = . scaleAspectFill ... Read More

Push Segue from UITableViewCell to ViewController in Swift

Samual Sam
Updated on 30-Jun-2020 05:50:40

717 Views

To create a segue from a UITableViewCell to another View controller, we’ll do it like any other ViewController to ViewController segue. We’ll do this with help of an example here.First Create a project, delete the View Controller from storyboard and add One Table View Controller and one View Controller in the storyboard.There will be one prototype cell in the Table View Controller by default. Click on it, go to its attribute inspector and give it “cell” as Identifier.Now from the prototype cell, press control and drag to the Second View Controller and select show from there.The storyboard should look like ... Read More

Extract Last 4 Characters from NSString

karthikeya Boyini
Updated on 30-Jun-2020 05:49:10

1K+ Views

To extract last 4 characters from a string in swift we can use the internal functions of String class in swift.With new release every time methods have been modified, deprecated, added and improved in swift. Swift provides different methods to achieve the same. Let’s see these ways of doing it with a few examples.Method 1 − SubstringsIn swift three we were allowed to use a method called substring in which we could pass the string, it’s last index and the offset from which we wanted to trim the string.Let’s see an example of the same:var Str1 = "12312$$33@" print(Str1.substring(from:Str1.index(Str1.endIndex, offsetBy: ... Read More

Send an Attachment in Email Using Swift

Samual Sam
Updated on 30-Jun-2020 05:44:32

636 Views

To send an email from our iPhone device using our application we need to import the MessageUI framework of iOS SDK. After importing the framework in your application, drag and drop a button on the view controller. Add empty action for that button.Now add the following code in your view controller.funccomposeEmail(to email: String, subject: String, Body: String) {    if( MFMailComposeViewController.canSendMail()) {       letmailComposer = MFMailComposeViewController()       mailComposer.mailComposeDelegate = self       mailComposer.setToRecipients([email])       mailComposer.setSubject(subject)       mailComposer.setMessageBody(Body, isHTML: true)       letpathPDF = "\(NSTemporaryDirectory())result.pdf"       if let fileData ... Read More

Access RESTful Services from iOS (iPhone)

karthikeya Boyini
Updated on 30-Jun-2020 05:43:40

185 Views

To access REST APIs in swift we need to go through a series of steps using the native way of networking in swift, that is using URL sessions and Data Tasks.Rest stands for Representational State Transfer, which defines some set of constraints that are to be used by web services. In swift, we can access web services in the following way.First of all, we need to create a session object, which default configuration.let configuration = URLSessionConfiguration.default let session = URLSession(configuration: configuration)Then we need to create an URL Request of The type we need, it can get, post, delete or put. ... Read More

Capture Picture from iOS Camera Using Swift

Samual Sam
Updated on 30-Jun-2020 05:35:25

3K+ Views

To capture pictures from a camera in swift we can use AVFoundation which is a framework in iOS SDK, but we should try to avoid using it until we need a lot of custom features in our camera application. In this example, we’ll only capture a picture from the camera and display it on the view. We’ll be using image picker in this example instead of AVFoundation.First, create a project and add an image view on its view Controller in the storyboard. Create the outlet in its class. Now inside the ViewController class conform it to −class ViewController: UIViewController, UIImagePickerControllerDelegate, ... Read More

What are Intent Filters in Android

George John
Updated on 30-Jun-2020 05:29:17

5K+ Views

An intent filter is an instance of the IntentFilter class. Intent filters are helpful while using implicit intents, It is not going to handle in java code, we have to set it up in AndroidManifest.xml. Android must know what kind of intent it is launching so intent filters give the information to android about intent and actions.Before launching intent, android going to do action test, category test and data test. This example demonstrate about how to use intent filters in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required ... Read More

Advertisements