#swift

iOS UIKit Animation Equivalent in Android's Kotlin Animation

by

Animation is a common feature we use often in our apps. iOS SDK does provide different ways to implement. From higher-level UIKit methods to more lower Core Animation. Any UIKit based animation is internally translated into core animations. UIKit methods abstracts the layer of core animation.

Today here we will see the higher-level UIKit animation’s equivalent in Android’s Kotlin - one of the ways that Android SDK provides us. We will use ViewPropertyAnimator - a simplified and optimised way to animate properties of a View in Android. A simple example to rotate a view by certain angle for a duration and also callback implementation when animation ends.

Below is the equivalent code snippet for Android Animation in Kotlin and iOS animation on views with UIKit.

CONTINUE READING

How to Present a View Modally From UITabBarController in Swift

by

Each tab of a tab bar controller is associated with a view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views. The tab bar interface displays tabs at the bottom for selecting between the different display views.

Tab bar controller lets us to implement easy multi-selection of displays easily. In this user interface style tabs at the bottom remains visible. Display switches between selection at child controller section above.

Comes a situation where view controller associated with a specific tab to display modally or push where Tabs at bottom gets covered behind this view controller. Instagram app is a good example where when create-post-tab is pressed, then display is opened over the tab controller.

How to present UITabBarItem’s view controller modally?

CONTINUE READING

How to Disconnect CoreBluetooth Peripheral cleanly in Swift

by

To disconnect a connected peripheral in CoreBluetooth we call method cancelPeripheralConnection


centralManager.cancelPeripheralConnection(peripheral)

This did disconnect peripheral in CoreBluetooth. In my program there is code that caches old discovered peripherals for certain period. It does go through cached peripherals to search for their services. What I found was that even if peripheral was disconnected code was somehow discovering the services and characteristics. There is a segment of code which is written to trigger certain behaviour when characteristics of peripheral is discovered. I did not want this for disconnected peripherals. Problem is not an issue for there are very simple solution for this scenario like checking the state of peripherals before trigger and etc.

I am emphasizing that Disconnected peripheral’s services still can be discovered in CoreBluetooth. Which is not an issue. I had assumed that when peripheral gets disconnected it removes its services and characteristics.

These methods do get called up even if peripheral is no more connected.


func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
CONTINUE READING

How to Change Color of UISwitch in Off State

by

There is no given individual property available to change the color of UISwitch in “off” state. We have the property to change color for “on” state called onTintColor. For "off" sate We can achieve by changing certain combination of properties to UISwitch.

By default background color of UISwitch is clear. We change background color to new color. Remember this changes the background color of UISwitch, and this will result in rectangle solid background color. Our UISwitch must have rounded corners and to match that we update the corner radius of UISwitch. That is it.

Optional step is to update tintColor of UISwitch to match the background color to keep it even.

First way, Inheritence - creating custom UISwitch class. In this example code I am exposing designable property for easy use in our storyboard.


class CustomSwitch: UISwitch{
    @IBInspectable var offTintColor: UIColor? {
        didSet {
            let minSide = min(bounds.size.height, bounds.size.width)
            layer.cornerRadius = minSide / 2
            backgroundColor = offTintColor
            tintColor = offTintColor
        }
    }
}

CONTINUE READING

How to Detect Backspace Event in UITextfield and UITextview?

by

In past I have used shouldChangeCharactersInRange delegate method to intercept the change in characters and make a near guess if it was delete-backspace that was tapped by the user. Guess was based on the nature of replacement character provided in method. If replacement character resembled the special escape character "\b" indicating backspace or empty character when view is already empty.

I felt above direction was a bit hacky. I found that based on response logic I was trying to write was giving me side effect of cursor always jumping to the end of text. For example when I tapped in between the characters of text to set the cursor in position and when I pressed backspace in keypad - I detected the event and did my processing to do some changes to text of UITextField and returned false.

What is the clean solution then?

CONTINUE READING

How to add a completion handler block for when I pop UIViewController?

by

Today I pondered about a problem which I had overlooked in past. How to add a completion handler block for when I pop UIViewController?


navigationController?.popViewController(animated: true)

In modal transition, dismissViewControllerAnimated, apple offers an option to call a completion block when dismissal of modal is finished. This is quite handy for various situations where you need to perform certain task like signalling a delegate about something when controller is removed from top of navigation or window.


dismiss(animated: true) {
}

Something similar isn’t offered directly in api for pop and push transitions. We do have been provided with viewDidAppear like methods that may be used to get things certain way. Flexibility and easiness that comes from completion handler of transition is unbeaten.

What we don’t have available from api can be solved by using core animation’s transactions. We group multiple animation related changes together and ensure all changes are run at the same time.

This is fine but how to notify when animation changes are finished?

CONTINUE READING

How to add a custom section header to a table view in Swift

by

Two of a simplest approaches is to use delegate method viewForHeaderInSection and return header view in a form of UIView itself or Custom class inheriting UITableViewHeaderFooterView.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

Asks the delegate for a view to display in the header of the specified section of the table view.

Using custom header view in the form of just UIView is good when we have something more than text to show in header.


override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.systemBlue
    return headerView
}

The other way - more flexible - is to have custom class of UITableViewHeaderFooterView. This approach gives much flexibility to have custom states and properties to handle for complex headers. Example code:

CONTINUE READING

UIToolbar for keyboard inputAccessoryView gives Layout Constraint Errors

by

UIToolbar is very useful control for our input fields for example when we need some action buttons above keypad for input whose keypad type is UIKeyboardType.phonePad. UIKeyboardType.phonePad doesn’t come up with any return key button to dismiss as of now. A simple UIToolbar is very sufficient to provide a button for user to dismiss or do something else.

UIToolbar

A control that displays one or more buttons along the bottom edge of your interface.

The simple code to add UItoolbar to textfield:


let flexibleButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem(title: "Dismiss", style: .done, target: self, action: #selector(doneKeypadTapped))
let bar = UIToolbar()
bar.items = [flexibleButton, done]
bar.sizeToFit()
phonePadTextfield.inputAccessoryView = bar

Now above code does give layout-constraints error indicating that few constraints are not satisfying each other and breaking the layout. It will not break the app in the sense but it does give sense of unease to a developer.

Small snippet from the contraint-error:


<NSLayoutConstraint:0x6000023bec60 ‘TB_Trailing_Trailing’ H:[_UIModernBarButton:0x7fb8bb990f20’Dismiss’]-(20)-|   (active, names: ‘|’:_UIButtonBarButton:0x7fb8bb98e100 )>

CONTINUE READING

How to find out distance between gps location coordinates in Swift?

by

A very simple way to calculate the distance between two given CLLocationCoordinate2D is to first convert them to MKMapPoint which represents two-dimensional point on map.


let point1 = MKMapPoint(clcoordinate1)
let point2 = MKMapPoint(clcoordinate2)

Now, we just use distance(to:) method to receive the distance in meters. Below is the code:

CONTINUE READING

sizeForItemAt of UICollectionView not working in Swift

by

There was a requirement where I was setting custom size for UICollectionViewCell. Custom size wasn’t dynamic but a static tall portrait ratio size. The effective way to set your own custom size is to call implement delegate method of protocol UICollectionViewDelegateFlowLayout:


func collectionView_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return estimatedCollectionSize
}

The problem:

UICollectionView was ignoring size instruction. It was sizing the cells to fit its content only.

CONTINUE READING

Recommended posts