Filling UITableView with data from bottom to top

If you work on something like an chat app, you may need to use the UITableView in a way where data is filled from bottom to top. An example of this is a chat detail screen, where you want the UITableView to show the latest messages at the bottom when loaded, new messages are added to the bottom and immediately shown and older messages are loaded on top when the user scrolls to the top of the UITableView.

There are multiple ways to achieve this, each with some advantages and disadvantages.

Scrolling

The first simplest idea that comes to mind is using the UITableView as is and just scrolling it when necessary:

  • Scroll to bottom when the initial messages are loaded
  • Scroll to bottom when a new message is added
  • When older messages are about to be added to the top, remember the position, add the older messages, scroll back to that position

The first two situations are easy to accomplish, but the last one is not. I could not find a way to make it works without a visible scrolling effect.

Rotating UITableView 180 degrees

Another solution is to rotate the UITableView by 180 degrees; rotating it upside down. Of course you have to also “flip” your data source but that is trivial to achieve. The advantage is that you do not have to do any scrolling when new messages are added to the bottom (which is the top of the rotated UITableView) and if you use batch updates instead of reload neither when older messages are loaded.

[Read More]

More readable XCode build output for CI

If you use Continuous Integration (CI) builds or build your app from the command line using xcodebuild you know that the output is not pretty and not very readable. Reading the build output is important when a CI build breaks, but it is not easy when it looks like this

Many iOS developers were not satisfied with this so the xcpretty project was created. Xcpretty is a fast and flexible formatter that turn the output from screnshot above to this neatly formatted output

[Read More]

iOS tip: Wireless debugging from XCode

One of the best XCode 9 features is the ability to deploy and debug iOS app on your device over WiFi, with no need to have the device connected to you computer by a cable. The only requirement is that the device runs iOS 11.

Setting it up is really easy. Connect the device using a cable like you normally do and go to Window | Devices and Simulators. You will see a new checkbox next to your iOS 11 devices called Connect via Network (see screenshot below), so check it. Now you can disconnect the cable and debug on your device over WiFi, the device has to be on the same network as your computer of course.

[Read More]

Fixing black artifact when changing large titles mode in iOS11

One of the new features of iOS 11 is the ability to display large headers in the navigation bar by setting the prefersLargeTitles property to true. You can set it for the whole app (using the UIAppearance for example) or differently for each view controller.

But there is a problem. If you navigate from a view controller with large titles enabled to a view controller with large titles disabled, you will see a black artifact under the change animation:

The black artifact comes from the navigation controller. If you set the backgroundColor of the navigation controller’s view to any, like red, it will replace the black artifact with an artifact of that color. The solution is to set the color of the color of you UI, white in my case:

[Read More]

Fixing problems with iPhone USB tethering on macOS

When my ISP had a problem resulting in Internet outage for multiple hours and I needed to work, I wanted to tether the LTE connection from my iPhone 6S to my hackintosh running macOS Sierra. It has no Wi-Fi card so the only was was tethering over USB cable.

The whole process should be easy, just connecting the iPhone to the computer with an USB cable and turning on the Personal hotspot in the Settings. The iPhone immediately registered 1 connection, but Internet did not work on the computer, although everything looked fine in System Preferences

[Read More]

Creating a dropdown menu from iOS navigation bar

Working on an iOS app I had to implement a filter for the table view displayed on screen. The filter should have contained 5 items and be accessible when tapping the screen title in the navigation bar. My first idea was to use an UIAlertController with those 5 options. It worked but it looked really ugly, So I started searching for a better, nicer solution. Ideally some kind of a dropdown menu.

I found multiple libraries for an iOS dropdown menu, but I liked BTNavigationDropdownMenu the best. The usage is really simple. First you define the items for the dropdown as an string array

create the menu instance

[Read More]

iOS tip: Changing navigation bar vs tab bar title

The UI of the iOS app I currently work on contains a tab bar with “Profile” as the title of one of the included tabs. This “Profile” tab contains a view controller with a navigation bar where I wanted the title to be set as “You profile”.

So I set the tab bar item’s title to “Profile” and wanted to set the navigation bar title of the view controller the standard way

I noticed that this also changes the title in the tab bar. After some research I found out that changing the view controller’s title property changes bot the title in the navigation bar and in the bar bar. But you can change the title just for the navigation bar

[Read More]

Using protocol default implementation instead of abstract classes

When I started using Swift the first thing I started missing was the support for abstract classes. I was used to using abstract classes for my ViewModels, especially to implement the template pattern, but also to provide some basic methods, like showing dialogs, that the ViewModels may need. Of course there are some “tricks” to do abstract classes like checking in the class type in constructor and throwing an error if it is the abstract class type, but Swift is a language based on protocols so there are better ways to achieve the same results just using the protocols.

Imagine you want to add a functionality to show iOS alerts to some of your ViewControllers. In a language like C# you would create an abstract class, something like BaseViewController, add a ShowAlert method to it and make all your ViewControllers inherit from this base class. Most languages nowadays only support single inheritance, so you would put all the functionality your ViewController may or may not need to your one abstract class. But you can typically implement as many interfaces as you like.

Using Swift and protocols there is another way. Protocols in Swift are really similar to interfaces in languages like Java or C# but with some neat stuff added to them like default protocol implementation.

[Read More]

Simpler and safer iOS custom table view cells with Reusable

When you create a UITableViewCell that you want to use with multiple UITableViews and design its view using a XIB file you know that registering and using it involves the usage of string constants. When you register your custom UITableViewCell with the UITableView you use a string as XIB name and a string as the reuse identifier. Then you use the string reuse identifier again to actually use it. There must be a better, safer way, right? Of course there is, just use Reusable.

Reusable is a Swift mixin for reusing views easily and in a type-safe way for UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards. It contains protocols you add to your classes and let the magic (the default implementation for those protocols) happen.

So how do you get rid of all those strings when using custom cells with UITableView? First, add the NibReusable protocol to you custom cell class

[Read More]

Making UITableView's header 'stickier'

Working on na iOS app I had to solve a interesting UI problem. The screen had to contain a UITableView with a header. The header should not have been visible when the screen was displayed. In fact the header should not have been visible when the user just scrolled the UITableView up and down, it only had to become visible when the user “dragged” the UITableView down, similar to doing pull to refresh. Scrolling the UITableView then hides the header again.

To better imagine the requirements, take a look at this animation

Notice that you see that the header is there but I have to really drag the UITableView to make it visible. It then disappears when I scroll the UITableView.

[Read More]