Skip to main content

UIBarButton Back Button

If you are using UINavigationController and segues, you may want to change the back button and in particular the title of the back bar button.

We set this is in the function prepareForSegue:sender:

Here we reference the UINavigationItem and assign it a newly created UIBarButtonItem.

 

Read More

Empty Table View Cells

Let’s imagine you have setup your UITableView and populated its cells. Things are looking good but there are empty cells after your content has finished? Well, there is an easy way to hide those empty cells.

Simply enter the following into ViewDidLoad() if you are using a UITableViewController or if your creating a UITableView then after you initialize your table view.

This will remove the empty cells from the bottom of your UITableView leaving just a blank space so ensure this still fits how you want the display to look.

Read More

Select a photo with UIImagePickerController

To select a photo from your library we can use the built in UIImagePickerController.

This is simple to use and allows you to select and use an image already saved to your device.

To start with we create a new UIImagePickerController class with:

let imagePickerController = UIImagePickerController()

We then assign its delegate and set its sourceType which below we set to PhotoLibrary so we can access our photos.

Finally we present the newly created controller using presentViewController.

The code above is wrapped in a button press action but is does not need to be.

To use the imagePickerController we need to implement the delegate UINavigationControllerDelegate and the UIImagePickerControllerDelegate.

We are going to need to implement a couple of delegate methods from the UIImagePickerControllerDelegate.

The first is imagePickerController:didFinishPickingMediaWithInfo

This method is used to select the image and close the controller. Here we use the image of imageSelected and assign it to the UIImageView photoImageView to display it.

We then close the UIImagePickerViewController with dismissViewController.

The second is imagePickerControllerDidCancel. This handles cancelling the imagePickerController.

You can view an example project of this code here

Read More

NSDate addition using NSCalendar

If you have an NSDate and want to add a time unit onto that date, then an option is to use the NSCalendar.

This will allow you to add to the date via the dateByAddingUnit, which allows you to add an NSCalendarUnit. These units could be for example: hours, days, months,  years, minutes.

Once you decide what unit type you want to add, you then set the value as an integer and the toDate date you want apply the calculation to.

To add an hour of time to an NSDate:

 

Read More

App Submission – Invalid Bundle Error

I recently started seeing the following error when submitting an previously fine app within Itunes Connect:

ERROR ITMS-90475: “Invalid Bundle. iPad Multitasking support requires launch story board in bundle ‘com.companyname.appname.'”

After some digging into this, it turned out rather obviously to do with the new multi-tasking within Xcode 7 and IOS 9 and so you need to specify how you want your app to handle that.

To solve this error in the short term. It was as simple as disabling the handling of it, by going to the “General” tab of your target and selecting that you require full screen.

T9VOj

 

Read More

UITableView willDisplayHeaderView

To amend the header section display in terms of colours for example on a uitableview you can utilise the UITableView Delegate protocol of  tableView:willDisplayHeaderView:forSection:

You can alter the background colour using the view tintColor and then by accessing the view you can amend the header text label.

You can apply different styles for different section headers using the section parameter making it very easy to amend the display of all headers within your table view.

 

Read More

UICollectionView Spacing

There are a few aspects to handling spacing and size of UICollectionViews and their cells.

Lets start with the spacing around the actual uicollectionview. To do this we effectively add padding or edge insets. You can add edges to all sides of the collection view.

Now what about the cells within the UICollectionView. We can set the size of these cells, and the spacing that appears between each cell.

These methods are part of the UICollectionViewDelegateFlowLayout which should be added as a protocol. This will create a grid effect by default which can then be amended using the methods below.

To set the cell size, we return the desired width and height of the cell as a CGSize object in the method collectionView:layout:sizeForItemAtIndexPath:.

To set the minimum space horizontally between each cell, we return a float value from the method collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

To set the minumum spacing vertically between the cells, or line height. We return a float value from the method collectionView:layout:minimumLineSpacingForSectionAtIndex:

A sample of the code can be found in a Playground file on GitHub

Read More

UICollectionViewCell Segue

When using a UICollectionView, you may want to pass data from one controller to another when a user clicks on the collection view cells. In order to do this, we need to find the item that has been clicked on.

With the uicollectionview and segue setup within the storyboard, you can use the prepareForSegue function to find the selected cell from the sender as shown below in the code extract.

Once you have the indexPath you are then able to get any relevant data from you collections using indexPath.row and pass that to the awaiting view controller.

As you will see above, the awaiting controller here has an embeded navigation controller, so we use this as the segue destination controller and then take the topViewController as our awaiting controller.

Read More

Change UITabBar Height

If you need to amend the UITabBar in order to fit your layout or design perfectly, a common place to start is with the height.

A quick and simple way I have been able to change the height is to subclass the UITabBar and then override the sizeThatFits(size: CGSize) function.

If you are using Storyboards, you can then set the class of the Tab Bar using the Custom Class option.

Below is a code example of the subclass:

Read More