Found 98 Articles for IPhone/iPad

How to display an image with rounded corners on iOS App using Swift?

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

2K+ 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

How to handle right-to-left and left-to-right swipe gestures on iOS App?

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

1K+ 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

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

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

818 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

665 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 create scrollable TextView on iOS App?

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

1K+ Views

To create a scrollable TextView in iOS we can do it in two ways, one by creating it using the storyboard and other by creating another textView programmatically.A text view is scrollable by default if it has text more than the height of textView and the scrollable property is disabled.1.Using storyboardGo to storyboard and from the object library drag a textView to your view.Now in the text view if the text is more than it’s height than it will be scrollable by default, otherwise it will not be scrollable.Give height constraint along with remaining required constraint.Make sure that scrolling enabled ... Read More

How to use UICollectionView in Swift?

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

349 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

3K+ 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 request permission programatically to use location services in iPhone/iOS?

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

165 Views

To request location services permission in ios with swift we can use the CLLocationManager.We’ll do this with help of a sample project. So, create a new project. First, we need to create a locationManager object, so in your view controller.var locationManager = CLLocationManager()Now, we, first of all, we need to check if the location services are enabled on the device or not. To check this we’ll useCLLocationManager.locationServicesEnabled() function, which returns a Boolean value showing whether the location service on the device is active or not.if CLLocationManager.locationServicesEnabled() {    print("permissions allowed") } else {    locationManager.requestAlwaysAuthorization()    locationManager.requestWhenInUseAuthorization() }In the example ... 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

Advertisements