Found 517 Articles for Swift

How to remove border in navigationBar in swift?

karthikeya Boyini
Updated on 30-Jun-2020 06:01:47

2K+ Views

To remove the border from a navigation bar in swift, we just need to add a few lines of code. Let’s see how the navigation bar looks when we run it without changing anything.Now let’s try to hide the line/ border shown in the above result.The navigation bar has two things that give it the default view of a grey shadow along with bottom line as shown above. One is the background image, and the other is the shadow image.First, we’ll hide the shadow image, by setting it to empty image and see how it looks.In your viewDidLoad add the ... Read More

How to detect swipe vertically on a ScrollView using Swift?

Samual Sam
Updated on 30-Jul-2019 22:30:24

607 Views

To detect swipe in scrollView we will need to make use of some tricks as scroll view does not natively give the directions of scroll made on it. We’ll see this with help of an example.Create an empty project, add scroll view to the view as per your requirement.Give them constraint as required in the application.From the object library, drag and drop a swipe gesture recognizer right above the Scroll View.Select the gesture recognizer, go to its attribute inspector and from there, select the swipe option and set the value as “up”.When you do this, now your gesture recognizer can ... Read More

How to add a Submit button after the end of the tableview using Swift?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

1K+ Views

To add a submit button at the end of a table view, we can make use of table view footers. Let’s see this with help of an example where we’ll add a footer view to our table, and inside the table, we will add code for adding button at the bottom of the table view.Create a new project first, then inside the view controller add the following code which will initialize the table, add a section and a few rows to the table.func initTableView() {    let tableView = UITableView()    tableView.frame = self.view.frame    tableView.dataSource = self    tableView.delegate ... Read More

How to load and display an image on iOS App using Swift?

Samual Sam
Updated on 30-Jul-2019 22:30:24

835 Views

To load and display an image in iOS app we’ll first need to get an image.Then we’ll drag that image to our project and select copy if required option and our application target.Let’s see the rest with help of an example.Now, we’ll create an UIImageView and assign the image to its image property, for that we’ll create a function.func addImage(imageName img: String) {    let imageView = UIImageView()    imageView.frame = self.view.frame    imageView.contentMode = .scaleAspectFit    if let newImage = UIImage(named: img) {       imageView.image = newImage    }    self.view.addSubview(imageView) }Now, we’ll call this code in ... Read More

How to create a Custom Dialog box on iOS App using Swift?

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

670 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

How to use UICollectionView in Swift?

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

360 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

How to give dynamic height to UIlabel programmatically in swift?

Samual Sam
Updated on 30-Jul-2019 22:30:24

3K+ Views

To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height.Let’s create a label and add it as a subview to our view.let label = UILabel() label.frame = CGRect(x: 10, y: 40, width: 200, height: 50) label.backgroundColor = colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1) label.textColor = colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1) label.text = "Custom label" self.view.addSubview(label)We can embed this in a function as well, and ... Read More

How to remove Specific Element from a Swift Array?

karthikeya Boyini
Updated on 30-Jun-2020 05:59:34

4K+ Views

To remove a specific object from an element in swift, we can use multiple ways of doing it. Let’s see this in the playground with help of an example.First, let’s create an array of String.var arrayOfString = ["a", "b", "c", "f"]We’ll do it with the following methods as shown below:Method 1 − Using the filter method of the array.Arrays in swift have a filter method, which filters the array object depending on some conditions and returns an array of new objects.let modifiedArray = arrayOfString.filter { $0 != "f" } print(modifiedArray)When we run the above code, we get the following result.Method ... Read More

How to use Swift to detect when AVPlayer video ends playing?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

2K+ Views

To detect the end of a video in swift we’ll need to create a video player, then use notifications to detect when the video stops playing. We’ll do this with help of an example in swift.Let’s create a project and drag and drop any video with extension “mp4”, select copy resource if required and add to the target of our project.Now we’ll programmatically first create a Video Player, then we’ll create url of the video in our project, and then we’ll play the video.var videoPlayer: AVPlayer!Now, in the viewDidAppear add the following code.override func viewDidAppear(_ animated: Bool) {    super.viewDidAppear(animated) ... Read More

How to use MBProgressHUD with swift?

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

To use MBProgressHUD in swift we first need to create a podfile if it does not exist already.Go to terminal and change directory to your project directory then initialize pod and later install MBProgressHUD.cd /projectDirectory pod init open podfileThen in the podfile add the following line and go back to the terminal and run the below command in the same directory.pod 'MBProgressHUD', '~> 1.1.0' pod installOnce you run these commands MBProgressHUD will be installed to your project, now you can import this library in ViewController where ever you want to use, or you may create an extension of UIView controller ... Read More

Advertisements